我是Vue的初学者(几个小时),在按下按钮后切换到下一个视图有困难。
login: function () -在严格模式代码中,函数只能在顶层或块内部声明。(10: 9)
谁能告诉我为什么会这样?
Login.vue
<template>
<div>
<button v-on:click="login">
</button>
</div>
</template>
<script>
methods: {
login: function () {
this.$router.push('/home')
}
}
</script>rouetr.js
import Vue from 'vue';
import VueRouter from 'vue-router';
import Home from '@/components/Home.vue';
import Login from '@/components/Login.vue'
Vue.use(VueRouter);
const routes = [
{
path: '/',
name: 'Home',
component: Home,
},
{
path: '/login',
name: 'Login',
component: Login
},
];
const router = new VueRouter({
routes,
});
export default router;发布于 2021-07-13 16:40:13
我认为这是一个linter错误,试着像这样声明你的函数:
<script>
methods: {
login () {
this.$router.push('/home')
}
}
</script>发布于 2021-07-13 18:58:39
我换了这个,它就醒了
<script>
export default {
name:"App",
methods: {
next() {
this.$router.push({ name: 'Home' })
console.log(this)
}
}
}
</script>https://stackoverflow.com/questions/68358630
复制相似问题