有个App.vue,需要引入HelloWorld.vue的组件
HelloWorld.vue
<template>
<div class="hello">
<h1>{{ msg }}</h1>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
props: {
msg: String
}
}
</script>
App.vue
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<HelloWorld msg="Welcome to Your Vue.js App"/>
<hello-world msg="Welcome to Your Vue.js App"></hello-world>
</div>
</template>
<script>
import HelloWorld from './components/HelloWorld.vue'
export default {
name: 'app',
components: {
HelloWorld
}
}
</script>
说明:要引入一个组件,有这么几个步骤:
1 导入:import HelloWorld from './components/HelloWorld.vue'
2 声明:components: { HelloWorld }
3 使用引入的组件:2种格式都可以
<hello-world></hello-world>或<hello-world></hello-world> //这里是将组件名按单词用‘-’分开,并且都是小写
<HelloWorld></HelloWorld>或<HelloWorld/> //这里就是组件本身定义的名字作为标签名