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

歡迎訪問 生活随笔!

生活随笔

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

vue

vue各种实例集合

發布時間:2025/3/17 vue 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 vue各种实例集合 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

注意:以下所有示例基于vue 2.x、Vuex 2.x、

vm.$mount()-掛載:

<body><div id="a"></div> </body> <script> var A = Vue.extend({ template: '<p>123</p>' });/*// 自動掛載new A({el: '#a'});*/var a = new A();a.$mount('#a')// 手動掛載 </script>

局部注冊:

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body> <div id="app"><com-b></com-b><com-c></com-c><com-d></com-d> </div><template id="com-d"><div>comD</div> </template><script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.js"></script> <script> var comC = Vue.component('comC', {template: '<div>comC</div>' });var vm = new Vue({el: '#app',components: {comB: {template: '<div>comB</div>'},comC: comC,comD: {template: "#com-d"}} }); </script> </body> </html>

動態組件:

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body> <div id="app"><component :is="cur"></component><button @click="change">change</button> </div><template id="comA"><div>comA</div> </template><script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.js"></script> <script> var vm = new Vue({el: '#app',data: {cur: {template: '<div>cur</div>'}},methods: {change: function(){this.cur = this.cur == 'comA' ? 'comB' : 'comA'}},components: {comA: {template: '#comA'},comB: {template: '<div>comB</div>'}} }) </script> </body> </html>

計算示例(computed):

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body> <div id="app">{{intro}}<input v-model="name"/><input v-model="age"/> </div><script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.js"></script> <script> var vm = new Vue({el: '#app',data: {name: 'Samve',age: 32},computed: {intro: function(){return 'name:' + this.name + ", age: " + this.age;}} }); </script> </body> </html>

自定義指令:實現"點擊按鈕使文本框獲取焦點"示例(directive)

<!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml" xmlns:v-on="http://www.w3.org/1999/xhtml"> <head><meta charset="UTF-8"><title>Title</title> </head> <body> <div id="app"><input v-focus="isFocus"/><button @click="change">change</button> </div><script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.js"></script> <script> Vue.directive('focus', {inserted: function(el, binding){if(binding.value){el.focus();}else{el.blur();}},componentUpdated: function(el, binding){if(binding.value){el.focus();}else{el.blur();}} });let vm = new Vue({el: '#app',data: {isFocus: true},methods: {change(){this.isFocus = !this.isFocus;}} }); </script> </body> </html>

使用jquery調用接口數據:

<!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml" xmlns:v-on="http://www.w3.org/1999/xhtml"> <head><meta charset="UTF-8"><title>Title</title> </head> <body> <div id="app"><div>{{list}}</div> </div><script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.js"></script> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script> <script> let vm = new Vue({el: '#app',data: {list: ''},created: function(){let that = this;$.ajax({url: 'http://v3.faqrobot.org/servlet/AQ?s=p&sysNum=14464304598886414&&sourceId=0×tamp=1473514741278&dataType=json',dataType: 'jsonp',success: function(data){that.list = data.webConfig.helloWord;}})} }) </script> </body> </html>

slot示例:

<!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml" xmlns:v-on="http://www.w3.org/1999/xhtml"> <head><meta charset="UTF-8"><title>Title</title> </head> <body> <div id="app"><com-a><p>中國科學院</p><p>院士</p><p slot="slotA">楊院士</p><com-b></com-b></com-a> </div><template id="comA"><div><slot></slot><slot></slot><slot name="slotA"></slot><P>comA</P></div> </template><template id="comB"><div>comB</div> </template><script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.js"></script> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script> <script> Vue.component('comA', {template: '#comA' });Vue.component('comB', {template: '#comB' });let vm = new Vue({el: '#app' }); </script> </body> </html>

vuex示例:

a. 簡單計數

<!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml" xmlns:v-on="http://www.w3.org/1999/xhtml"> <head><meta charset="UTF-8"><title>Title</title> </head> <body> <div id="app"><div>{{num}}</div><div><button @click="add">add</button></div><div><button @click="reduce">reduce</button></div> </div><script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.js"></script> <script src="https://cdn.bootcss.com/vuex/3.0.1/vuex.js"></script> <script> Vue.use(Vuex);let myStore = new Vuex.Store({state: {num: 0},mutations: {add: function(state){state.num++;},reduce: function(state){state.num--;}} });let vm = new Vue({el: '#app',store: myStore,computed: {num: function(){return myStore.state.num;}},methods: {add(){myStore.commit('add');},reduce(){myStore.commit('reduce');}} }) </script> </body> </html>

b. 子組件獲取Vuex狀態:

<!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml" xmlns:v-on="http://www.w3.org/1999/xhtml"> <head><meta charset="UTF-8"><title>Title</title> </head> <body> <div id="app"><div>{{num}}</div><com-a></com-a> </div><script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.js"></script> <script src="https://cdn.bootcss.com/vuex/3.0.1/vuex.js"></script> <script> Vue.use(Vuex);let myStore = new Vuex.Store({state: {num: 0} });let vm = new Vue({el: '#app',store: myStore,// 把store實例注入所有的子組件computed: {num(){return this.$store.state.num;// 使用this.$store即可引用s}},components: {comA: {template: `<div>子: {{num}}</div>`,computed: {num(){return this.$store.state.num;// 使用this.$store即可引用}}}} }) </script> </body> </html> 參考:https://blog.csdn.net/u011500781/article/details/52475967

轉載于:https://www.cnblogs.com/samve/p/9424800.html

總結

以上是生活随笔為你收集整理的vue各种实例集合的全部內容,希望文章能夠幫你解決所遇到的問題。

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