今天描述的問題估計會有很多人也遇到過。
vue-router多個路由地址綁定一個組件造成created不執行
也就是文檔描述的,如下圖

我的解決方案:
created () { console.log(this.getStatus(this.$route.path)) this.userpath() //我要執行的函數 }, methods: { getStatus (urlStr) { var urlStrArr = urlStr.split('/') return urlStrArr[urlStrArr.length - 1] } }, watch: { '$route' (to, from) { console.log(this.getStatus(this.$route.path)) this.userpath() //再次調起我要執行的函數 } }
vue之watch用法
項目中剛好也用到了需要檢測某值是否發生了變化,獲取最新的值。就分享一下
vue單文件組件寫法: <template> //觀察數據為字符串或數組 <input v-model="example0"/> <input v-model="example1"/> //當單觀察數據examples2為對象時,如果鍵值發生變化,為了監聽到數據變化,需要添加deep:true參數 <input v-model="example2.inner0"/> </template> <script> export default { data(){ return { example0:"", example1:"", example2:{ inner0:1, innner1:2 } } }, watch:{ example0(curVal,oldVal){ console.log(curVal,oldVal); }, example1:'a',//值可以為methods的方法名 example2:{ //注意:當觀察的數據為對象或數組時,curVal和oldVal是相等的,因為這兩個形參指向的是同一個數據對象 handler(curVal,oldVal){ conosle.log(curVal,oldVal) }, deep:true } }, methods:{ a(curVal,oldVal){ conosle.log(curVal,oldVal) } } } </script>
項目中還遇到了需要使用md5加密,分享一個不錯的網址http://blog.csdn.net/qq_35844177/article/details/70597597
