zoukankan      html  css  js  c++  java
  • vue 子组件接收父组件的另一种方法

    子组件获取父组件传递的数据通常是通过props属性接收父组件的传递过来的数据,代码如下:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    </head>
    <body>
    <div id="app">
    <my-child :msg='msg'></my-child>
    </div>
    </body>
    </html>
    <script src="./node_modules//vue//dist//vue.min.js"></script>
    <script>
    let vm = new Vue({
    el: '#app',
    data: {
    msg: 'helloword'
    },
    components: {
    'myChild': {
    props: ['msg'],
    mounted() {
    console.log(this.$attrs)
    },
    components: {
    'myChildren': {
    props: ['msg'],
    template:
    `<span>{{msg}}</span>
    `
    }
    },
    template: `<div>{{msg}}
    <my-children :msg='msg'></children>
    </div>`
    }
    }
    })
    </script>
    也可以通过子组件的$attrs接收的父组件的数据,但是这时候传递的数据子组件中不能有props的属性,不然子组件的$attrs获得的是空对象,代码如下:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    </head>
    <body>
    <div id="app">
    <my-child :msg='msg'></my-child>
    </div>
    </body>
    </html>
    <script src="./node_modules//vue//dist//vue.min.js"></script>
    <script>
    let vm = new Vue({
    el: '#app',
    data: {
    msg: 'helloword'
    },
    components: {
    'myChild': {
    // props:['msg'],
    mounted() {
    console.log(this.$attrs)
    },
    components: {
    'myChildren': {
    //props:['msg'],
    template:

    `<span>{{this.$attrs.msg}}</span>
    `
    }
    },
    template: `<div>{{this.$attrs.msg}}
    <my-children :msg='$attrs.msg'></children>
    </div>`
    }
    }
    })
    </script>
  • 相关阅读:
    黑苹果崩溃恢复
    黑苹果声音小解决方法
    idea plugin 进度条
    phpstorm 插件
    awesome mac
    webstorm vue eslint 自动修正配置
    Laravel/php 一些调试技巧
    php ZipArchive 压缩整个文件夹
    laravel 模型事件 updated 触发条件
    php 开启 opcache 之后 require、include 还会每次都重新加载文件吗?
  • 原文地址:https://www.cnblogs.com/zhx119/p/11096643.html
Copyright © 2011-2022 走看看