动态双向绑定
<select v-model="blog.author">
<option v-for="author in authors">
{{author}}
</option>
</select>
//====================================
data () {
return {
authors:["hel","hh","lll"]
}
},
触发内容,传递给后台
npm install vue-resource --save
下载npm run dev
重启main.js
添加import VueResource from 'vue-resource'
和Vue.use(VueResource)
http://jsonplaceholder.typicode.com/
<!-- 触发时,post到后台 -->
<button v-on:click.prevent="post">添加博客</button>
//=====================
methods:{
post:function(){
this.$http.post("https://jsonplaceholder.typicode.com/posts",{
title:this.blog.title,
boby:this.blog.content,
userId:1
})
.then(function(data){
console.log(data);
});
}
当数据发送成功
<form v-if="!submmited">
created(){
this.$http.get('https://jsonplaceholder.typicode.com/posts')
.then(function(data){
// console.log(data);
// 只展示10条数据
this.blogs = data.body.slice(0,10);
console.log(this.blogs);
})
- 定义一个指令,改变标题颜色
main.js
加钩子函数//ShowBlogs.vue
<h2 v-rainbow>{{blog.title}}</h2>
//App.vue
Vue.directive('rainbow',{
bind(el,binding,vnode){
el.style.color = "#"+Math.random().toString(16).slice(2,8);
}
})
背景颜色
<div v-theme:column="'narrow'" id="show-blogs">
Vue.directive('theme',{
bind(el,binding,vnode){
if(binding.value == 'wide'){
el.style.maxWidth = "1260px"
}else if(binding.value == 'narrow'){
el.style.maxWidth = "560px";
}
if(binding.arg == 'column'){
el.style.background = "#6677cc";
el.style.padding = '20px';
}
}
})
使用filter,把标题变为大写
<h2 v-rainbow>{{blog.title | to-uppercase}}</h2>
加过滤器main.js
里定义Vue.filter("to-uppercase",function(value))
to-uppercase
是名字,value
是传过来的值// 过滤器
Vue.filter("to-uppercase",function(value){
return value.toUpperCase();
})
让文字变为小写
Vue.filter("snippet",function(value){
return value.slice(0,100)+"...";
})
搜索功能
<input type="text" v-model="search" placeholder="搜索">
2.data里加seach
data(){
return{
blogs:[],
search:""
}
},
<div v-for="blog in filteredBlogs" class="single-blog">
computed: {
filteredBlogs:function(){
return this.blogs.filter((blog)=>{
// 没保存返回false
return blog.title.match(this.search);
})
}
}
把全局api变为局部api
export default {
filters: {
"to-uppercase":function(value){
return value.toUpperCase();
}
}
}
filters: {
toUppercase(value){
return value.toUpperCase();
}
},
局部颜色变化
directives:{
'rainbow':{
bind(el,binding,vnode){
el.style.color = "red";
}
}
}