【问题标题】:Vue.js: Why is my Single File Component not being rendered/displayed in my templateVue.js:为什么我的单文件组件没有在我的模板中呈现/显示
【发布时间】:2019-10-30 21:40:40
【问题描述】:

我目前正在学习如何使用 Vue.js 构建 Web 应用程序。我使用了 vue-cli(版本 3.8.2)来搭建 HelloWorld 应用程序。

我将生成的HelloWorld.vue组件修改如下:

/path/to/vue-app/src/components/HelloWorld.vue

<template>
  <div class="hello">
        <b-tabs content-class="mt-3">
            <b-tab title="module1" active>
                <Module1/>
            </b-tab>
        </b-tabs>
    </div>
</template>

<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
import Module1 from './Module1.vue';

@Component
export default class HelloWorld extends Vue {
  @Prop() private msg!: string;
}
</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>

/path/to/vue-app/src/components/Module1

<template>
    <div class="module1">
        Module1
    </div>
</template>

<script>
    import { Component, Prop, Vue } from 'vue-property-decorator';

@Component
export default class Module1 extends Vue {
}    
</script>

<style>
</style>

/path/to/vue-app/src/router.ts

import Vue from 'vue';
import Router from 'vue-router';
import Home from './views/Home.vue';

Vue.use(Router);

export default new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      name: 'home',
      component: Home,
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import(/* webpackChunkName: "about" */ './views/About.vue'),
    },
  ],
});

当我导航到http://localhost:8080/ 时,我的模块Module1 的内容没有显示在页面上。

为什么组件没有被渲染(显示),我该如何解决这个问题?

【问题讨论】:

    标签: vue.js vuejs2


    【解决方案1】:

    您必须在使用之前注册您的Module1 组件(奇怪的是您没有收到任何错误)。 在打字稿中,您必须在 @Component 装饰器中注册组件,如下所示:

    @Component({
      components: {
        Module1
      }
    })
    

    【讨论】:

    • 这段代码进入哪个源文件? Module.vue 还是 HelloWorld.vue?
    • 在父组件内部,或者如果您打算全局使用该组件,您可以在 main.ts 中注册该组件
    猜你喜欢
    • 2018-09-17
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-27
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    相关资源
    最近更新 更多