如何从脚本代码重定向到另一个vue页面。我使用的是router.push(),但不能重定向到我想要的vue页面。
以下是我的源代码。
src/router/index.js
import Vue from 'vue'
import Router from 'vue-router'
import HomePage from '@/components/HomePage'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'IndexPage',
component: IndexPage
},
{
path: '/homepage',
name: 'HomePage',
component: HomePage
}
]
})src/components/IndexPage.vue
<script>
import VueRouter from 'vue-router'
export default {
name: 'IndexPage',
methods: {
redirectUser() { // this method is called on button click
if (1 == 1)
{
router.push('/homepage');
//this.$router.push('/homepage');
}
}
}
}
</script>在运行这段代码之后,我得到了一个错误,该错误声明:
ReferenceError:路由器未定义在eval中
src/main.js
import Vue from 'vue'
import App from './App'
import router from './router'
Vue.config.productionTip = false
window.Vue = Vue
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})此外,我可以从浏览器http://localhost:8080/#/homepage访问相同的链接。但不能从我的脚本代码重定向到它。
发布于 2018-07-11 13:38:20
谢谢你的反馈朋友。我没有在vue上导入我的路由器文件。更新后的代码行是:
src/components/IndexPage.vue
<script>
import router from '../router/index.js' // Just added this line and code works !!
export default {
name: 'IndexPage',
methods: {
redirectUser() { // this method is called on button click
if (1 == 1)
{
router.push({name: 'HomePage'})
}
}
}
}
</script>https://stackoverflow.com/questions/51281157
复制相似问题