网站搜索

typescript - 如何修复使用 jsx/tsx 渲染时出现的未知自定义 Vuetify 元素错误?


我刚接触 Vue 和 Vuetify。我正在使用 TSX——即 TypeScript JSX——创建一个简单的应用。应用在浏览器中运行时控制台出现以下错误

[Vue 警告]:未知的自定义元素: - 你是否正确注册了该组件?对于递归组件,请确保提供“name”选项。

发现于

---> 在 src/App.vue

[Vue 警告]:未知的自定义元素: - 你是否正确注册了该组件?对于递归组件,请确保提供了 "name" 选项。

发现于

---> 在 src/App.vue

...

这里是 App.vue

<script lang="tsx">
import { Component, Vue } from "vue-property-decorator";

@Component
export default class App extends Vue {
  render() {
    return (
      <div id="app">
        <v-app>
          <v-content>
            <p>Hello World</p>
          </v-content>
        </v-app>
      </div>
    );
  }
}
</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>

main.ts

import Vue from 'vue'
import './plugins/vuetify'
import App from './App.vue'

Vue.config.productionTip = false

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

插件/vuetify.ts

import Vue from 'vue'
import Vuetify from 'vuetify/lib'
import 'vuetify/src/stylus/app.styl'

Vue.use(Vuetify, {
  iconfont: 'md',
})

你可以在下面的 GitHub 链接中找到完整的项目文件 https://github.com/janucaria/vuetify-tsx-demo

答案#1

在你的 App.vue 中,如下导入所需的 Vuetify 组件:

<script lang="tsx">
import { Component, Vue } from "vue-property-decorator";
import { VApp, VContent } from 'vuetify/lib';

@Component({
  components: {
   'v-app': VApp,
   'v-content': VContent
  }
})
export default class App extends Vue {
  render() {
    return (
      <div id="app">
        <v-app>
          <v-content>
            <p>Hello World</p>
          </v-content>
        </v-app>
      </div>
    );
  }
}
</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>