0
在的index.html:如何在index.html中獲取vue-router?
<script>
function demoFunction(name) {
this.$router.push({name: 'Succ'})
}
爲什麼this.$router
是undefined
?我怎樣才能得到vue-router
?謝謝
在的index.html:如何在index.html中獲取vue-router?
<script>
function demoFunction(name) {
this.$router.push({name: 'Succ'})
}
爲什麼this.$router
是undefined
?我怎樣才能得到vue-router
?謝謝
貌似你在全局範圍內定義了這個函數,所以this
裏面的函數會指向瀏覽器中的全局對象window
。 window
沒有屬性$router
,這就是您未定義的原因。
您可以使用this.$router
裏面只有你VUE選項和太:
,如果你創建了一個新的路由器,比如像這樣
var router = new VueRouter({
routes:[
//your route objects
]
});
,然後通過這臺路由器實例作爲財產到根vue實例
new Vue({
el: '#app',
router: router
});
謝謝!我設置了窗口。$ router = this。$ router – tanpuer