如何从我的脚本代码重定向到另一个vue页面。我正在使用router.push(),但无法重定向到我想要的vue页面。
以下是我的源代码。
src/路由器/索引。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:评估时未定义路由器
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.但无法从脚本代码重定向到它。
谢谢朋友们的反馈。我没有在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>
使用您的组件实例属性访问路由器:
this.$router.push({name: 'HomePage'})
你的应用程序中有吗?
new Vue({
router,
render: h => h(App)
}).$mount('#app')
导入Vue和VueRouter,然后调用
Vue.use(VueRouter)
然后用你的方法,
this.$router.push({name: 'HomePage'})
编辑
如果您想在代码中使用Vue和Vue路由器,您需要同时导入它们,这就是为什么在eval中没有定义路由器的原因。而且还使用
this.$router.push('/homepage');
在src/components/IndexPage中试试这个。vue
<script>
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
export default {
name: 'IndexPage',
methods: {
redirectUser() { // this method is called on button click
if (1 == 1)
{
this.$router.push('/homepage');
}
}
}
}
</script>
我正在尝试使用react router(版本^1.0.3)做一个简单的重定向到另一个视图的操作,我只是觉得有些累。 我只需要将用法发送到“/login”,就这样。 我能做什么? 控制台中的错误: 未捕获的引用错误:未定义PropType 用我的路线归档
问题内容: 我正在尝试使用react-router( 版本^ 1.0.3 )重定向到另一个视图,这让我很累。 我所需要的只是将使用发送到’/ login’就是这样。 我能做什么 ? 控制台错误: 未捕获的ReferenceError:未定义PropTypes 用我的路线归档 问题答案: 对于简单的答案,您可以使用来自的组件,而不是。有一些方法可以更改JS中的路由,但是似乎您在这里不需要。 要在1.
问题内容: 我正在使用ajax调用在服务文件中执行功能,并且如果响应成功,我想将页面重定向到另一个URL。目前,我正在通过使用简单的js“ window.location = response[‘message’];”来做到这一点。但是我需要用angularjs代码替换它。我看过关于的各种解决方案,他们使用了$location。但是我是新手,对实现它有困难。 问题答案: 您可以使用Angular
在我的应用程序中,用户登录后我有一个主页和一些其他页面。问题是,当我在其中一个页面中刷新页面时,它会再次将我发送到主页。这是我的路线: 应用程序组件具有路由器出口 那么,我期待什么呢?首先,如果我是我的“列表”页面()并且我刷新了这个页面,它应该留在那里。在那一页。但现在它将我重定向到本地主机:4200/home。当然,当我单击一个列表项时,它应该将我发送到本地主机:4200/detail/ite
问题内容: 如何使用jQuery或纯JavaScript将用户从一个页面重定向到另一页面? 问题答案: 一个不只是简单地使用jQuery进行重定向 jQuery不是必需的,[ ]它将最好模拟HTTP重定向。 比使用更好,因为它不会将原始页面保留在会话历史记录中,这意味着用户不会陷入永无休止的后退按钮惨败中。 如果要模拟某人单击链接,请使用 如果要模拟HTTP重定向,请使用 例如: