methods_弹窗
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"></meta> <title>index</title> <script src="https://unpkg.com/vue"> </script>
</head> <body>
<div id ="app"> <button v-on:click="sayHi"> click me </button>
</div> <script> var vm = new Vue({ el: "#app", data: { message:"this'a a blog" }, methods: { sayHi: function(event){ alert(this.message); }
} }); </script> </body> </html>
|
双向数据绑定
view 通过 <input v-model
= “item”>将用户的输入绑定到名外item的数据上
通过,绑定显示后台名为item的数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"></meta> <title>index</title> <script src="https://unpkg.com/vue"> </script>
</head> <body>
<div id="watch-example"> <p> Ask a yes/no question: <input v-model="question"> </p> <p> {{ answer }} </p> </div>
<script src="https://cdn.jsdelivr.net/npm/axios@0.12.0/dist/axios.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/lodash@4.13.1/lodash.min.js"></script> <script> var watchExampleVM = new Vue({ el: '#watch-example', data: { question: '', answer: 'I cannot give you an answer until you ask a question!' }, watch: { question: function (newQuestion, oldQuestion) { this.answer = 'Waiting for you to stop typing...' this.debouncedGetAnswer() } }, created: function () { this.debouncedGetAnswer = _.debounce(this.getAnswer, 500) }, methods: { getAnswer: function () { if (this.question.indexOf('?') === -1) { this.answer = 'Questions usually contain a question mark-"?" . ;)' return } this.answer = 'Thinking...' var vm = this axios.get('https://yesno.wtf/api') .then(function (response) { vm.answer = _.capitalize(response.data.answer) }) .catch(function (error) { vm.answer = 'Error! Could not reach the API. ' + error }) } } }) </script>
</body> </html>
|
vue组件
用户自定义的结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"></meta> <title>index</title> <script src="https://unpkg.com/vue"> </script>
</head> <body> <div id="app"> <qinjiang v-for="item in items" v-bind:qin="item"> </qinjiang> </div> <script> Vue.component('qinjiang',{ props: ['qin'], template: '<li>{{qin}}</li>' });
var vm = new Vue({ el: "#app", data: { items: ["java","linux","前端"] } }); </script> </body> </html>
|
Axios异步通信
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"></meta> <title>index</title> <script src="https://unpkg.com/vue"> </script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head> <body> <div id="vue"> 点我 <div> {{info.name}} </div> <div> {{info.address.street}} </div> </div>
<script type="text/javascript"> var vm = new Vue({ el: '#vue', data(){ return{ info:{ name : null address:{ street: null, city: null, country: null } } } }, mounted(){ axios.get('data.json').then(response=>(this.info=response.data));
} }) </script> </body> </html>
|
对应的json文件data.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| { "name" : "狂神说java", "url" : "htp://baidu.com", "page" : 1, "isNonProfit" : true, "address" : { "street": "含光门", "city" : "陕西西安", "country": "中国" }, "links": [ { "name" : "B站", "url": "https://www.bilibili.com/" }, { "name": "4399", "url": "https://www.4399.com/" }, { "name": "百度", "url": "https://www.baidu.com/" } ] }
|
计算属性
计算属性的重点突出在属性两个字上,首先它是一个属性,其次它有计算的能力,这里计算
就是一个函数;它能够将计算结果缓存起来.
内存中执行,虚拟Dao
计算属性的主要特征就是为了将不经常变化的计算结果进行缓存,以节约我们的系统开销.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"></meta> <title>index</title> <script src="https://unpkg.com/vue"> </script>
</head> <body> <div id="vue"> <p> {{currentTime1()}}</p> <p> {{currentTime2}}</p> </div>
<script type="text/javascript"> var vm = new Vue({ el: '#vue', data:{ message: "hello!!" }, methods:{ currentTime1: function(){ return Date.now(); } }, computed: { currentTime2: function(){ return Date.now(); } } }) </script> </body> </html>
|
VUE插槽(slot)//内容分发
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"></meta> <title>index</title> <script src="https://unpkg.com/vue"> </script>
</head> <body> <div id="vue"> <todo> <todo-title slot="todo-title" :title="title"></todo-title> <todo-items slot="todo-items" v-for="item in todoItems" :item="item"></todo-items> </todo> </div>
<script type="text/javascript"> Vue.component("todo",{ template: '<div>\ <slot name="todo-title"></slot>\ <ul>\ <slot name="todo-items"> </slot>\ </ul>\ </div>' }); //通过指定solt的name来绑定组件 Vue.component("todo-title",{ props:['title'], template:'<div>{{title}}</div>' });
Vue.component("todo-items",{ props:['item'], template: '<li>{{item}}</li>' }); var vm = new Vue({ el:"#vue", data:{ title: "what do you like?", todoItems: ["happiness","movie","runnig","reading"] } }); </script> </body> </html>
|
自定义事件
通过以上代码不难发现,数据项在Vue的实例中,但删除操作要在组件中完成,那么组件如何才能删除Vue实例中的数据呢?此时就涉及到参数传递与事件分发了,Vue为我们提供了自定义事件的功能很好的帮助我们解决了这个问题;
使用this.$emit (‘自定义事件名’,参数)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"></meta> <title>index</title> <script src="https://unpkg.com/vue"> </script>
</head> <body> <div id="vue"> <todo> <todo-title slot="todo-title" :title="title"></todo-title> <todo-items slot="todo-items" v-for="(item,index) in todoItems" :item="item" v-bind:index="index" v-on:remove="removeItems(index)" ></todo-items> </todo> </div> <script type="text/javascript"> Vue.component("todo",{ template: '<div>\ <slot name="todo-title"></slot>\ <ul>\ <slot name="todo-items"> </slot>\ </ul>\ </div>' }); Vue.component("todo-title",{ props:['title'], template:'<div>{{title}}</div>' });
Vue.component("todo-items",{ props:['item','index'], template: '<li>{{item}} <button @click="remove"> delete </button></li>' , methods: { remove: function (index){ this.$emit('remove',index) } }
});
var vm = new Vue({ el:"#vue", data:{ title: "what do you like?", todoItems: ["happiness","movie","runnig","reading"] }, methods:{ removeItems: function(index){ this.todoItems.splice(index,1); } } }); </script> </body> </html>
|