组件中使用指令和事件

错误写法
data: {
    msg: 'hello world'
   }
data必须是一个函数
<!DOCTYPE html>
<html lang="en">
<head>
    <title></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="/vue2.js"></script>
</head>
<body>
    <template id='dan'>
            <div>{{msg}}<button @click='add'>点击</button>
            </div>
            
        </template>
    <div id="app">
        <index-a></index-a>
    </div>
    <script>
        Vue.component('indexA', {
            template: '#dan',
            data() {
                return {
                    msg: "3333"
                }
            },
            methods: {
                add() {
                    this.msg = '555'
                }
            }
        })
        var vm = new Vue({
            el: '#app',
            data: {
            }
        })
    </script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_42442123/article/details/85762375