日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > vue >内容正文

vue

Vue 组件与复用

發布時間:2023/12/2 vue 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Vue 组件与复用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

(1)全局注冊

<!DOCTYPE html> <html lang="zh"><head><meta charset="UTF-8" /><title>Vue</title></head><body><div id="app"><my-component></my-component></div><script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script><script type="text/javascript">//示例前注冊 Vue.component('my-component', {//DOM結構必須被元素包含 template: '<div>組件內容</div>'})new Vue({el: "#app"})</script></body></html>

?

(2)局部注冊

<!DOCTYPE html> <html lang="zh"><head><meta charset="UTF-8" /><title>Vue</title></head><body><div id="app"><my-component></my-component></div><script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script><script type="text/javascript">var Child = {template: '<div>組件內容</div>'}new Vue({el: "#app",components: {'my-component': Child}})</script></body></html>

(3)is掛載組件

table、ul、ol、select這些標簽會限制其內的元素,這時可以使用is來掛載組件

<!DOCTYPE html> <html lang="zh"><head><meta charset="UTF-8" /><title>Vue</title></head><body><div id="app"><table><tbody is='my-component'></tbody></table></div><script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script><script type="text/javascript">//示例前注冊 Vue.component('my-component', {//DOM結構必須被元素包含 template: '<div>組件內容</div>'})new Vue({el: "#app"})</script></body></html>

(4)組件也可以有data,method,computed等屬性。但是data是函數,數據需要return出去。

<!DOCTYPE html> <html lang="zh"><head><meta charset="UTF-8" /><title>Vue</title></head><body><div id="app"><my-component></my-component></div><script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script><script type="text/javascript">//示例前注冊 Vue.component('my-component', {//DOM結構必須被元素包含 template: '<div>{{message}}</div>',data: function() {return {message: '組件內容'}}})new Vue({el: "#app"})</script></body></html>

(5)解決多個組件之間數據共享問題

給組件返回一個新的data對象

<!DOCTYPE html> <html lang="zh"><head><meta charset="UTF-8" /><title>Vue</title></head><body><div id="app"><my-component></my-component><my-component></my-component><my-component></my-component></div><script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script><script type="text/javascript">//示例前注冊 Vue.component('my-component', {//DOM結構必須被元素包含 template: '<button @click="counter ">{{counter}}</button>',data: function() {return {counter: 0}}})new Vue({el: "#app"})</script></body></html>

?(6)props傳遞數據、events觸發事件和slot內容分發構成Vue組件的3個API來源


更多專業前端知識,請上 【猿2048】www.mk2048.com

總結

以上是生活随笔為你收集整理的Vue 组件与复用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。