实现单页面开发的方法
- 通过hash记录跳转的路径(可以产生历史管理)
- 浏览器自带的历史管理的方法history(history.pushState(’’,’’,’/d’))可能会导致404错误
开发的时候我们会使用hash的方式因为不会产生404错误,在上线的时候我们用history的方式
自定义指令
- 使用directives,binding为传值
//html标签中
<button v-color ="flag">变色</button>
//在vue实例中
directives:{
color(el,bindings){
el.style.background = binding.value
}
data:{
flag:‘red’
}
}
深度监控
- 平常的watch只能监控一层数据的变化,比如对象的删改,但是对象里的数组变化并不能触发触发todos,所有如下用handler来深度触发。
watch:{
todos:{
handler(){//默认写成函数 就相当于写成handler
localStorage.setItem('data',JSON.stringify(this.todos))//存入localStorage(默认存入的是字符串)
},deep:true
}
}
//如果localStorage有的就用有的,没有就用默认的
created(){
this.todos = JSON.parse(localStorage.getItem(data)) || this.todos
}