• vue3组件传值defineProps/回调defineEmits/子组件属性defineExpose/'defineProps' is not defined/Unexpected 'debugger' statement nodebugger/nounusedvars/is assigned a value but never used


    使用defineProps进行父子组件传值,报异常:'defineProps' is not defined,没法用,看了很多文章都说配置在vue.config.js中:我的vue-cli版本是5.0.8  ,2022-08月装的。
    module.exports = {
      env: {
        "node":true,
        "vue/setup-compiler-macros": true
      }
    }
    

    可是我配置进去,报没有 env 的错误,后来发现在package.json中有此配置

    "eslintConfig": {
        "root": true,
        "env": {
          "node": true,
          "vue/setup-compiler-macros": true
        },
    }
    

      把这一段配置加到这里就可以使用了,https://eslint.vuejs.org/user-guide/#installation

    父组件:用组合式api很简洁的啊,别用老一套了。

    <template>
      <input
        type="text"
        v-model="message"
      />
      <test-com
        :address='message'
        @handle='callbackHandle'
        ref='childComponentInfo'
      />
      <br />
      <button @click="getchildComponentInfo()">UserName</button>
    </template>
    <script setup>
    // 组合式api
    import { ref } from "vue";
    import TestCom from "./components/TestCom.vue";
    const message = ref("Hello Vue!");
    const childComponentInfo = ref(null);//定义一个属性,指定为子组件实例,可以访问子组件暴露出的属性
    const callbackHandle = (addr) => {
      console.log("parent:" + addr);
    };
    const getchildComponentInfo = () => {
      console.log("userName:"+childComponentInfo.value.userInfo.userName);
      console.log("stuName:"+childComponentInfo.value.stuInfo.stuName);
    };
    
    </script>
    // <script>
    // 选项式api
    // import HelloWorld from "./components/HelloWorld.vue";
    // export default {
    //   name: "App",
    //   components: {
    //     // HelloWorld,
    //     TestCom,
    //   },
    //   methods: {
    //     callbackHandle(addr) {
    //       console.log("parent:" + addr);
    //     },
    //   },
    //   data() {
    //     return {
    //       message: "Hello Vue!",
    //     };
    //   },
    // };
    //
    </script>

    子组件:

    <script setup>
    import {reactive} from 'vue'
    // const props = defineProps(['address']);//写法1
    //写法2,带类型
    const props = defineProps({
      address: String,
    });
    const emits = defineEmits(["handle"]);
    const userInfo = reactive({userName:"jay.star",age:33});
    const stuInfo = reactive({stuName:"jack",age:22});
    defineExpose({userInfo,stuInfo});//暴露子组件的属性,父组件可以直接访问
    const btnClick = function () {
      console.log("child:" + props.address);
      emits("handle", props.address);
    };
    </script>
    <template>
      <div>
        {{props.address}}
        <br />
        {{userInfo}}
        <button @click="btnClick()">ShowName</button>
      </div>
    </template>
    

      方便。。。https://blog.csdn.net/qq_43185384/article/details/125208794
    https://cn.vuejs.org/guide/essentials/template-refs.html#ref-on-component

    问题:Unexpected 'debugger' statement no-debugger,package.json 中的rules节点加入可调试的配置,这样就可以在js中加debugger来调试了

    "eslintConfig": {
        "root": true,
        "env": {
          "node": true,
          "vue/setup-compiler-macros": true
        },
        "extends": [
          "plugin:vue/vue3-essential",
          "eslint:recommended"
        ],
        "parserOptions": {
          "parser": "@babel/eslint-parser"
        },
        "rules": {
          "no-debugger": "off",
          "no-console": "off"
        }
      },
    

      已声明的变量或函数没有被使用,会报错 obj is assigned a value but never used,加上配置 no-unused-vars 可以忽略,避免报错

    "rules": {
          "no-debugger": "off",
          "no-console": "off",
          "no-unused-vars": [
            "error",
            {
              "varsIgnorePattern": ".*",
              "args": "none"
            }
          ]
        }
    

      

  • 相关阅读:
    Golang 单例模式 singleton pattern
    golang可见性规则(公有与私有,访问权限)
    golang init方法和main方法初始化顺序
    Golang的面向对象编程【结构体、方法、继承、接口】
    Convert Sorted List to Binary Search Tree——将链表转换为平衡二叉搜索树 &&convert-sorted-array-to-binary-search-tree——将数列转换为bst
    C/C++程序到内存分配(转)
    有关C/C++指针的经典面试题(转)
    C结构体之位域(位段)(转)
    Balanced Binary Tree——数是否是平衡,即任意节点左右字数高度差不超过1
    Path SumI、II——给出一个数,从根到子的和等于它
  • 原文地址:https://www.cnblogs.com/xsj1989/p/16602057.html
Copyright © 2020-2023  润新知