日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > vue >内容正文

vue

如何在一个文件中写多个Vue组件(译-有删改)

發布時間:2025/7/14 vue 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何在一个文件中写多个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组件(译-有删改)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。