如何在一个文件中写多个Vue组件(译-有删改)
原文地址 Writing multiple Vue components in a single file
在一個文件中編寫多個組件是React的模式,其中一些文件包含多個組件。
走開發過程中,有些組件對文件/導出組件是“私有的”,因為沒有其他組件需要使用它們。這個時候我們傾向于把它們寫到一個文件中。
- 一. 使用render函數
- 二. 使用Vue.component和template
- 三. 僅使用template而不使用Vue.component
- 四.使用JSX(編譯為渲染函數)
我們將使用vue-cli腳手架項目中的默認“Hello World”組件作為示例。
默認情況下,有兩個文件,App和HelloWorld,HelloWorld接收msg屬性并呈現它。
要將它們寫在單個文件中,如果用React實現的話一般如下所示:
const HelloWorld = ({ msg }) => (<div><h1>Hello world</h1><div>{msg}</div> </div>);const App = () => (<div id="app"><HelloWorld msg="Welcome to Your React App" /> </div>);export default App;由于React代碼實際上就是普通的JavaScript,因此您可以在一個文件中定義多個組件。
在Vue中,它仍然是可能的,但它有點復雜,有多種方法可以實現:

- 一. 使用render函數
- 二. 使用Vue.component和template
- 三. 僅使用template而不使用Vue.component
- 四.使用JSX(編譯為渲染函數)
示例倉庫:vue-multiple-components-in-sfc。
一. 使用render函數
<template><div id="app"><HelloWorld msg="Welcome to Your Vue.js App"/></div> </template> <script> // inline component const HelloWorld = {props: ['msg'],render(h) {return h('div', [h('h1', 'Hello world'),h('div', this.msg)])} }; export default {name: 'app',components: {HelloWorld} } </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>二. 使用Vue.component和template
<template><div id="app"><HelloWorld msg="Welcome to Your Vue.js App"/></div> </template> <script> import Vue from 'vue'; // inline component with template string ? const HelloWorld = Vue.component('hello-world', {props: ['msg'],template: `<div><h1>Hello world</h1><div>{{ this.msg }}</div></div>` }); export default {name: 'app',components: {HelloWorld} } </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>但是這種方式不能在Vue Runtime-only構建版本上生效,會有如下的錯誤提示

關于Vue幾種不同構建版本的區別可以參考:Explanation of Different Builds
上面的問題可以使用帶compiler的Vue構建版本來修復
例如:
//Webpack module.exports = {// ...resolve: {alias: {'vue$': 'vue/dist/vue.esm.js' // 'vue/dist/vue.common.js' for webpack 1}} }或者vue.config.js中添加:
module.exports = {runtimeCompiler: true };三. 僅使用template而不使用Vue.component
<template><div id="app"><HelloWorld msg="Welcome to Your Vue.js App"/></div> </template> <script> // inline component with template string ? const HelloWorld = {props: ['msg'],template: `<div><h1>Hello world</h1><div>{{ this.msg }}</div></div>` }; export default {name: 'app',components: {HelloWorld} } </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>這個方法也需要帶compiler的Vue構建版本
四.使用JSX(編譯為渲染函數)
我們可以用JSX重寫我們的初始渲染函數示例 App.js:
<template><div id="app"><HelloWorld msg="Welcome to Your Vue.js App"/></div> </template> <script> // inline component with JSX const HelloWorld = {props: ['msg'],render() {return (<div><h1>Hello world</h1><div>{this.msg}</div></div>);} };export default {name: 'app',components: {HelloWorld} } </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>Vue CLI 3+已經配置了babel-plugin-transform-vue-jsx
Using JSX with Vue and Why You Should Care
轉載于:https://www.cnblogs.com/star91/p/ru-he-zai-yi-ge-wen-jian-zhong-xie-duo-gevue-zu-ji.html
總結
以上是生活随笔為你收集整理的如何在一个文件中写多个Vue组件(译-有删改)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 面向对象博客作业3
- 下一篇: docker的网络模式