Я не могу двигаться другим путем в Vue JS - PullRequest
1 голос
/ 28 сентября 2019

Я новичок в Vue JS, и я делаю простую страницу в Vue JS.Вот мои коды:

main.js

import Vue from 'vue'
import App from './App.vue'
import PokeProfile from './components/PokeProfile.vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import VueRouter from 'vue-router';

Vue.use(VueRouter)
Vue.use(ElementUI)

const router = new VueRouter({
  routes: [
    {path: '/', component: App},
    {path: '/pokemon/:id', component: PokeProfile},
  ],
  mode: 'history'
})

//Vue.config.productionTip = false

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

App.js

<template>
  <div id="app">
    <div class="tag-group">
      <el-tag
          v-for="pokemon in pokemons"
          :key="pokemon.national_id"
          :type="pokemon.name"
          effect="plain">
          <poke-item :pokemon=pokemon></poke-item>
        </el-tag>
    </div>
  </div>
</template>

<script>
import PokeItem from './components/PokeItem.vue'
import axios from 'axios'

export default {
  name: 'app',
  components: {
    PokeItem
  },
  data() {
    return {
      pokemons: []
    }
  },
  created() {
    axios.get("http://localhost:3000")
    .then(res => this.pokemons = res.data)
    .catch(err => {console.log(err)})
  }
}
</script>

<style>
div {
    display: flex;
    justify-content: center;
}
</style>

PokeItem.js

<template>
    <div>
      <router-link :to="pokemonLink">
        {{pokemon.name}}
      </router-link>
    </div>
</template>

<script>
export default {
    data() {
        return {}
    },
    props: {
        pokemon: {
            type: Object,
            required: true
        }
    },
    computed: {
        pokemonLink() {
            return `/pokemon/${this.pokemon.national_id}`
        }
    }
}
</script>

PokeProfile.js

<template>
    <h1>Hello Pokemon</h1>
</template>
<script>
export default {
}
</script>

Проблема в том, что я не могу перейти к PokeProfile.js, когда нажимаю на элемент в файле PokeItem.js.В чем может быть проблема?Я проверил раздел кода, связанный с маршрутизацией, но не увидел никаких проблем.

1 Ответ

2 голосов
/ 28 сентября 2019

Vue-Router использует динамический компонент (<router-view>) для визуализации компонентов ваших маршрутов.Обычно вы найдете этот компонент в шаблоне вашего app.vue.Поскольку у вас нет <router-view> компонента, Vue-Router не знает, где визуализировать ваши компоненты маршрута.

Попробуйте:

// main.js

import Home from './components/Home.vue'

const router = new VueRouter({
  routes: [
    {path: '/', component: Home},
    {path: '/pokemon/:id', component: PokeProfile},
  ],
  mode: 'history'
})

// components/Home.vue

// your old App.vue

// ./App.vue
<template>
  <main>
    <router-view></router-view>
  </main>
</template>
...