この記事では主に 3 つの Vue コンポーネントの記述形式を詳しく紹介します。興味のある方は参考にしてください。
この記事の例は参考のために Vue コンポーネントの記述形式を共有します。次のように
最初のはスクリプトタグを使用します
<!DOCTYPE html> <html> <body> <p id="app"> <my-component></my-component> </p> <-- 注意:使用<script>标签时,type指定为text/x-template,意在告诉浏览器这不是一段js脚本,浏览器在解析HTML文档时会忽略<script>标签内定义的内容。--> <script type="text/x-template" id="myComponent">//注意 type 和id。 <p>This is a component!</p> </script> </body> <script src="js/vue.js"></script> <script> //全局注册组件 Vue.component('my-component',{ template: '#myComponent' }) new Vue({ el: '#app' }) </script> </html>
2番目のはテンプレートタグを使用します
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <p id="app"> <my-component></my-component> </p> <template id="myComponent"> <p>This is a component!</p> </template> </body> <script src="js/vue.js"></script> <script> Vue.component('my-component',{ template: '#myComponent' }) new Vue({ el: '#app' }) </script> </html>
3番目の単一ファイルコンポーネント
この方法は、vueの単一ページアプリケーションで一般的に使用されます。詳細については、公式 Web サイトを参照してください: https://cn.vuejs.org/v2/guide/single-file-components.html
接尾辞 .vue、コンポーネント Hello.vue を持つファイルを作成し、コンポーネント フォルダー
<template> <p class="hello"> <h1 id="nbsp-msg-nbsp">{{ msg }}</h1> </p> </template> <script> export default { name: 'hello', data () { return { msg: '欢迎!' } } } </script>
app.vue
<!-- 展示模板 --> <template> <p id="app"> <img src="/static/imghwm/default1.png" data-src="./assets/logo.png" class="lazy" alt="Vue コンポーネントを記述するにはどのような方法がありますか?" > <hello></hello> </p> </template> <script> // 导入组件 import Hello from './components/Hello' export default { name: 'app', components: { Hello } } </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>
上記は私がまとめたもので、将来的には皆さんのお役に立てれば幸いです。
関連記事:
JavaScriptを使用して左側のメニュー効果を実装する方法
方法Javascriptメカニズムを使用してカスタムイベントを実装します
以上がVue コンポーネントを記述するにはどのような方法がありますか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

vue中props可以传递函数;vue中可以将字符串、数组、数字和对象作为props传递,props主要用于组件的传值,目的为了接收外面传过来的数据,语法为“export default {methods: {myFunction() {// ...}}};”。

如何覆盖组件库样式?下面本篇文章给大家介绍一下React和Vue项目中优雅地覆盖组件库样式的方法,希望对大家有所帮助!

本篇文章带大家聊聊vue指令中的修饰符,对比一下vue中的指令修饰符和dom事件中的event对象,介绍一下常用的事件修饰符,希望对大家有所帮助!

react与vue的虚拟dom没有区别;react和vue的虚拟dom都是用js对象来模拟真实DOM,用虚拟DOM的diff来最小化更新真实DOM,可以减小不必要的性能损耗,按颗粒度分为不同的类型比较同层级dom节点,进行增、删、移的操作。


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

SublimeText3 中国語版
中国語版、とても使いやすい

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

メモ帳++7.3.1
使いやすく無料のコードエディター

ドリームウィーバー CS6
ビジュアル Web 開発ツール
