什么是组件?
组件是 vue 应用程序的构建块。每个组件都有自己的功能和视图,组件可以在整个应用程序中重用。组件的一个示例是可以在不同页面上访问的导航栏。
创建基本组件
在组件文件夹中创建一个名为 helloworld.vue 的新组件文件(如果需要,您可以更改文件名)(如果尚不存在,则创建一个新的组件文件夹)。
helloworld 组件:
<template><div>
<h1>hello, world!</h1>
</div>
</template><script>
export default {
name: 'helloworld'
}
</script><style scoped>
h1 {
color: blue;
}
</style>
<template><div id="app">
<helloworld></helloworld>
</div>
</template><script>
import HelloWorld from './components/HelloWorld.vue'; //adjust according to the path to your component
export default {
components: {
HelloWorld
}
}
</script>
现在您应该能够看到 app.vue 组件中渲染的 helloworld 组件。