vue学习【第五篇】:Vue组件

什么是组件

- 每一个组件都是一个vue实例
- 每个组件均具有自身的模板template,根组件的模板就是挂载点
- 每个组件模板只能拥有一个根标签
- 子组件的数据具有作用域,以达到组件的复用

根组件

<div id="app">
    <h1>{{ msg }}</h1>
</div>
<script type="text/javascript">
	// 通过new Vue创建的实例就是根组件(实例与组件一一对应,一个实例就是一个组件)
	// 每个组件组件均拥有模板,template
	var app = new Vue({
		// 根组件的模板就是挂载点,不需要自定义
		el: "#app",
		data : {
			msg: "根组件"
		},
		// 模板: 由""包裹的html代码块,出现在组件的内部,赋值给组件的$template变量
		// 显式书写模块,就会替换挂载点,但根组件必须拥有挂载点
		template: "<div>显式模板</div>"
	})
	// app.$template
</script>

局部组件

<body>
    <div id="app">
        <abc></abc>
        <abc></abc>
        <abc></abc>
    </div>
    <hr>
    <div id="main">
        <local-tag></local-tag>
        <local-tag></local-tag>
    </div>
</body>
<script src="js/vue.js"></script>
<script>
    // 局部组件
    var localTag = {
        // 子组件的数据具有作用域,以达到组件的复用, 每一个复用的组件具有自身独立的一套数据
        data: function () {
            return {  // 返回值是一个数据字典(一套数据)
                count: 0
            }
        },
        template: "<div @click='fn'>点击{{ count }}次</div>",
        methods: {
            fn: function () {
                this.count += 1;
            }
        }
    }

    // app根组件
    new Vue({
        el: "#app",
        // 注册
        components: {
            'abc': localTag
        }
    })
    // main根组件
    new Vue({
        el: "#main",
        components: {
            // localTag    js中键值一样可以简写,只写一个就行     这样写比较装十三,一般不知道js标识符于css/html标识符的相互转换的可能不知道
            'local-tag': localTag
        }
    })
</script> 

全局组件

<div id="app">
    <global-tag></global-tag>
    <global-tag></global-tag>
</div>
<script>
	Vue.component('global-tag', {
		data () {
			return {
				count: 0
			}
		},
		template: '<button @click="btnAction">全局{{ count }}</button>',
		methods: {
			btnAction () {
				this.count ++
			}
		}
	})
    new Vue({
        el: "#app"
    })
</script>

父组件传递数据给子组件

通过绑定属性的方式进行数据传递

<div id="app">
    <global-tag :sup_data1='sup_data1' :supData2='sup_data2'></global-tag>
</div>
<script type="text/javascript">
	Vue.component('global-tag', {
		props:['sup_data1', 'supdata2'],
		template: '<div>{{ sup_data1 }} {{ supdata2 }}</div>'
	})
	new Vue({
		el: '#app',
		data: {
			sup_data1: '数据1',
			sup_data2: '数据2'
		}
	})
</script>

子组件传递数据给父组件

通过发送事件请求的方式进行数据传递

<div id="app">
    <global-tag @send_action='receiveAction'></global-tag>
</div>
<script type="text/javascript">
	Vue.component('global-tag', {
		data () {
			return {
				sub_data1: "数据1",
				sub_data2: '数据2'
			}
		},
		template: '<div @click="clickAction">发生</div>',
		methods: {
			clickAction () {
				this.$emit('send_action', this.sub_data1, this.sub_data2)
			}
		}
	})
	new Vue({
		el: '#app',
		methods: {
			receiveAction (v1, v2) {
				console.log(v1, v2)
			}
		}
	})
</script>

父子组件实现todoList

<div id="app">
    <div>
        <input type="text" v-model="val">
        <button type="button" @click="submitMsg">提交</button>
    </div>
    <ul>
        <!-- <li v-for="(v, i) in list" :key="i" @click="removeMsg(i)">{{ v }}</li> -->
        <todo-list v-for="(v, i) in list" :key="i" :v="v" :i="i" @delect_action="delect_action"></todo-list>
    </ul>
</div>
<script type="text/javascript">
	Vue.component("todo-list", {
		template: "<li @click='delect_action'><span>第{{ i + 1 }}条: </span><span>{{ v }}</span></li>",
		props: ['v', 'i'],
		methods: {
			delect_action () {
				this.$emit("delect_action", this.i)
			}
		}
	})
	
	new Vue({
		el: "#app",
		data: {
			val: "",
			list: []
		},
		methods: {
			submitMsg () {
				// 往list中添加input框中的value
				if (this.val) {
					this.list.push(this.val);
					this.val = ""
				}
			},
			delect_action(index) {
				this.list.splice(index, 1)
			}
		}
	})
</script>

猜你喜欢

转载自www.cnblogs.com/596014054-yangdongsheng/p/10385622.html