【问题标题】:Vue js does not change route - vue route does not workVue js 不改变路由——vue 路由不起作用
【发布时间】:2021-07-18 04:18:20
【问题描述】:

main.js

import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
//Components
import Homepage from './components/Homepage.vue'
import Login from './components/Login.vue'
import RegistrationCompany from './components/RegistrationCompany.vue'

Vue.use(VueRouter);
Vue.config.productionTip = false


const routes = [
  { path : '/', name :'Homepage', component : Homepage },
  { path : '/login', name :'login', component : Login },
  { path : '/registration_company', name :'RegistrationCompany', component : RegistrationCompany },

]

const router = new VueRouter({
  mode: "history",
    routes
  
})

new Vue({
  router,
  render: h => h(App),
}).$mount('#app')

主页.vue

    <template>
  <div class="homepage">
    <h1>Home</h1>
    <p>
      <router-link to="/login">Go to Login</router-link>
      <router-link to="/registration_company">Go to Registration</router-link>
    </p>
  </div>
</template>

<script>
export default {
  name: "Homepage",
  props: {},
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>

App.js

<template>
  <div id="app">
    <Homepage />
  </div>
</template>

<script>
import Homepage from "./components/Homepage.vue";

export default {
  name: "App",
  components: {
    Homepage,
  },
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

一切正常,只工作了一次,但我不知道主js有什么变化,它又停止工作了

这是一个简单的代码,在主代码中我声明了路由,在主页中我放置了链接标签以重定向到组件。

但这些都不起作用,安装正常使用 npm 完成

【问题讨论】:

    标签: javascript vue.js frameworks vue-component vue-router


    【解决方案1】:

    您似乎缺少router-view

    <template>
      <div class="homepage">
        <h1>Home</h1>
        <p>
          <router-link to="/login">Go to Login</router-link>
          <router-link to="/registration_company">Go to Registration</router-link>
        </p>
        <router-view />
      </div>
    </template>
    

    【讨论】:

    • omg 非常感谢,是的,app.vue 和其他组件中缺少 。
    猜你喜欢
    • 2018-03-31
    • 2021-06-02
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    • 2020-07-03
    • 2017-09-22
    • 2018-03-26
    相关资源
    最近更新 更多