詳解Vue 全局變量,局部變量
局組件和局部組件
1.先定義組件 Vue.component('組件名', { 組件模板對象 })
注意: 組件名不要使用原生的標簽名, 若組件名定義時用的是駝峰命名法, 則調(diào)用時用中劃線分割后小寫
例如: 組件-->mtText 使用時--> <my-text></my-text>
2.配置組件的模板 注意: 組件的模板內(nèi)容有且只有一個根元素
3.在視圖層里調(diào)用 ,用雙標簽
4.組件是一個獨立的作用域, 也可以看成一個特殊的vue實例, 可以有data, methods,computed等等
注意: 組件的data是函數(shù), 函數(shù)中需要返回一個對象作為組件的data
全局組件案例
<body> <div id="app"> <my-component></my-component> </div> <script src="lib/vue-2.4.0.js"></script> <script> //全局組件 Vue.component('myComponent',{ //1.組件的內(nèi)容/模板 template: '<div><div>頭部組件</div><h1 @click="fn">呵呵{{msg}}</h1></div>', data(){ return { msg:'hello,組件' } }, methods:{ fn(){ console.log(this.msg); } } }) let vm = new Vue({ el:"#app", data:{ }, methods:{ }, }) </script> </body>
局部組件案例
<body> <div id="app"> <my-component></my-component> <my-test></my-test> </div> <template id="box1"> <h1>haha</h1> </template> <template id="box2"> <div> <ul> <li v-for="item in arr"> {{ item }} </li> </ul> </div> </template> <script src="lib/vue-2.4.0.js"></script> <script> let vm = new Vue({ el:"#app", data:{ }, methods:{ }, //局部子組件 components:{ // 組件名: {配置項} "myComponent":{ template:'#box1', data(){ return { msg:"哈哈" } } }, "myTest":{ template:"#box2", data(){ return { arr:[1,2,3,4] } } } } }) </script> </body>
組件切換:法一
<body> <div id="app"> <a href="" @click.prevent=" rel="external nofollow" rel="external nofollow" flag=true">登錄</a> <a href="" @click.prevent=" rel="external nofollow" rel="external nofollow" flag=false">注冊</a> <login v-if="flag"></login> <register v-else="flag"></register> </div> <script src="lib/vue-2.4.0.js"></script> <script> Vue.component("login",{ template:"<h1>登錄組件</h1>" }) Vue.component("register",{ template:"<h1>注冊組件</h1>" }) let vm = new Vue({ el:"#app", data:{ flag: false }, methods:{ }, }) </script> </body>
組件切換:法二
<style> .red{ color:red; } .v-enter{ opacity:0; transform: translateX(150px); } .v-leave-to{ opacity:0; transform: translateX(-150px); } .v-enter-active, .v-leave-active{ transition: all 0.5s; position: absolute; } </style>
<body> <div id="app"> <a href="" :class=" rel="external nofollow" rel="external nofollow" {red: flag=='login'}" @click.prevent="flag='login'">登錄</a> <a href="" :class=" rel="external nofollow" rel="external nofollow" {red: flag=='register'}" @click.prevent="flag='register'">注冊</a> <!-- vue提供了一個標簽 component標簽(理解為一個占位符), 用來展示對應(yīng)名稱的組件 :is屬性設(shè)置指定的組件名 --> <transition> <component :is="flag"></component> </transition> </div> <script src="lib/vue-2.4.0.js"></script> <script> Vue.component("login",{ template:"<h1>登錄組件</h1>" }) Vue.component("register",{ template:"<h1>注冊組件</h1>" }) let vm = new Vue({ el:"#app", data:{ flag: "login" }, methods:{ }, }) </script> </body>
父組件向子組件傳值
<body> <div id="app"> <my-component :fromfather="father"></my-component> </div> <template id="box1"> <h1 @click="change"> {{ fromfather }} 子組件的數(shù)據(jù) </h1> </template> <template id="grandSon"> <h1>孫子組件的數(shù)據(jù)</h1> </template> <!--1.子組件不能訪問父組件的數(shù)據(jù) 2. 解決辦法: ①在引用子組件時, 通過屬性綁定 v-bind方法, 把需要傳遞給子組件的數(shù)據(jù)以綁定的形式傳過來 ② 在子組件配置項里添加 props: ['傳遞過來的數(shù)據(jù)']--> <script src="lib/vue-2.4.0.js"></script> <script> let vm = new Vue({ el:"#app", data:{ father:'啊~~這是父組件的數(shù)據(jù)' }, methods:{ }, //局部子組件 components:{ // 組件名: {配置項} "myComponent":{ template:'#box1', data(){ return { msg:"哈哈" } }, //在子組件配置項里添加 props: ['傳遞過來的數(shù)據(jù)'] //注意: 組件中所有的props中的數(shù)據(jù), 都是通過父組件傳遞給子組件的, props中的數(shù)據(jù)是只讀, 無法修改 props:['fromfather'], methods:{ change(){ // this.fromfather = "被修改了" } }, //局部子子組件 components:{ 'grandSon':{ template:'#grandSon' } } } } }) </script> </body>
以上所述是小編給大家介紹的Vue全局變量局部變量詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
在elementui中Notification組件添加點擊事件實例
這篇文章主要介紹了在elementui中Notification組件添加點擊事件實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11vue學(xué)習(xí)筆記之給組件綁定原生事件操作示例
這篇文章主要介紹了vue學(xué)習(xí)筆記之給組件綁定原生事件操作,結(jié)合實例形式詳細分析了vue.js組件綁定原生事件相關(guān)原理、實現(xiàn)方法與操作注意事項,需要的朋友可以參考下2020-02-02關(guān)于vue.js中實現(xiàn)方法內(nèi)某些代碼延時執(zhí)行
今天小編就為大家分享一篇關(guān)于vue.js中實現(xiàn)方法內(nèi)某些代碼延時執(zhí)行,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11element-table如何實現(xiàn)自定義表格排序
這篇文章主要介紹了element-table如何實現(xiàn)自定義表格排序,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07