【Vue.js】 todolist组件拆分(八)

2018-09-02  本文已影响0人  kingloongMagic

1、component全局组件

Vue.component('todo-item',{
            template:'<li>item</li>'
        })

2、局部组件

A、定义局部组件

var Todo-item={
            template:'<li>item</li>'
        })

B、局部组件需要到Vue中声明引用

component:{
  'todo-item'=Todo-item
}

3、外部向组件传参

A、外部定义:content="item",传参
B、组件中定义props:["content"]接收参数
C、在组件模板中参数化引用template:'<li>{{content}}</li>'

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Vue学习</title>
    <script src="./vue.js"></script>
</head>
<body>
    <div id="root">
        <div v-if="show">hello</div>
        <input v-model="inputValue"/>
        <button @click="handleClick">click</button>
        <ul>
            <todo-item v-for="(item, index) of list"
                       :key="index"
                       :content="item"
            >
            </todo-item>
        </ul>
    </div>
    <script>
        Vue.component('todo-item',{
            props:['content'],
            template:'<li>{{content}}</li>'
        })
        new Vue({
            el:"#root",
            data:{
                show:true,
                list:[]
            },
            methods:{
                handleClick:function () {
                    this.list.push(this.inputValue);
                    this.inputValue = "";
                }
            },
        })
    </script>
</body>
</html>
上一篇 下一篇

猜你喜欢

热点阅读