如何在Vue路线中使用组件?

时间:2019-10-16 21:16:01

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

我正在尝试在家庭路线中使用Vue组件<channel-card></channel-card>,但遇到了很多错误。

main.js

const router = new VueRouter({
  routes: [
    {path: '/home', component: home}
  ],
  mode: 'history'
});

home.vue路线

<template>
  <div class="home">
    <main class=" bg-white w-full h-100vh shadow-md p-2">
      <channel-card></channel-card>
    </main>
  </div>
</template>
<script>
export default {
  name: 'home',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App'
    }
  }
}
</script>

错误

vue.runtime.esm.js?2b0e:619 [Vue warn]: Unknown custom element: <channel-card> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

found in

---> <Home> at src/components/home.vue
       <App> at src/App.vue
        <Root>

ChannelCard.vue

export default {
  name: 'ChannelCard',
  props: {
    msg: String
  }
}

修改

我还尝试将其添加到我的home.vue路线:

import ChannelCard from './partials/ChannelCard.vue';
export default {
  name: 'home',
  components: { ChannelCard },
  data () {
    return {
      msg: 'Welcome to Your Vue.js App'
    }
  }
}

我收到此错误:

  ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/b中的

./ partials / ChannelCard.vue   abel-loader / lib!./ node_modules / cache-loader / dist / cjs.js ?? ref--0-0!./ node_modules / vue-loader / lib ?? vu   e-loader-options!./ src / components / home.vue?vue&type = script&lang = js&

1 个答案:

答案 0 :(得分:0)

您需要注册<channel-card>组件并将其添加到components的脚本部分的home.vue中:

home.vue

<template>
  <div class="home">
    <main class=" bg-white w-full h-100vh shadow-md p-2">
      <channel-card></channel-card>
    </main>
  </div>
</template>

<script>
import channelCard from './channel-card.vue';

export default {
  name: 'home',
  components: { channelCard },
  data () {
    return {
      msg: 'Welcome to Your Vue.js App'
    }
  }
}
</script>