Creating vue components is to split the code amount of vue instances and find different functions through different components. Here are three ways to create components in Vue. Friends in need can refer to them. I hope it will be helpful to everyone.
Method 1: Use Vue.extend to create a global Vue component
Instance:
var com1 = Vue.extend({ template: '<h3 id="这是使用-nbsp-Vue-extend-nbsp-创建的组件">这是使用 Vue.extend 创建的组件</h3>' // 通过 template 属性,指定了组件要展示的HTML结构 })
Use Vue.component
('component name', created component object) When creating a vue component, use the first letter of the name to name it. When referencing the component, change the uppercase to lowercase letters, and use # before the two words. ##- Connection; parameter one: a tag to introduce the component, parameter two:
template is the
HTML content displayed by the component.
<div id="app"> <!-- 如果要使用组件,直接,把组件的名称,以 HTML 标签的形式,引入到页面中,即可 --> <mycom1></mycom1> </div> <script> Vue.component('mycom1', Vue.extend({ template: '<h3 id="这是使用-nbsp-Vue-extend-nbsp-创建的组件">这是使用 Vue.extend 创建的组件</h3>' })) // 创建 Vue 实例,得到 ViewModel var vm = new Vue({ el: '#app', data: {}, }); </script>The result obtained is:
Method 2: Use Vue.component directly
<body> <div id="app"> <!-- 使用标签形式,引入自己的组件 --> <mycom2></mycom2> </div> <script> Vue.component('mycom2', { template:`<div> <h3 id="这是直接使用-nbsp-Vue-component-nbsp-创建出来的组件">这是直接使用 Vue.component 创建出来的组件</h3> <span>123</span> </div> ` }) // 创建 Vue 实例,得到 ViewModel var vm = new Vue({ el: '#app', data: {}, methods: {} }); </script> </body>Result:
Method 3: Use Vue.component outside the controlled #app, and use the template element
inside the controlled #App Outside, use the template element to define the HTML template structure of the component<body> <div id="app2"> <login></login> <mycom3></mycom3> <login></login> </div> <template id="tmpl"> <div> <h1 id="这是通过-nbsp-template-nbsp-元素-在外部定义的组件结构-这个方式-有代码的只能提示和高亮">这是通过 template 元素,在外部定义的组件结构,这个方式,有代码的只能提示和高亮</h1> <h4 id="好用-不错">好用,不错!</h4> </div> </template> <template id="tmpl2"> <h1 id="这是私有的-nbsp-login-nbsp-组件">这是私有的 login 组件</h1> </template> <script> Vue.component('mycom3', { template: '#tmpl', }) // 创建 Vue 实例,得到 ViewModel var vm2 = new Vue({ el: '#app2', data: {}, methods: {}, filters: {}, directives: {}, components: { // 定义实例内部私有组件的 login: { template: '#tmpl2' } }, }) </script> </body>Output result:
vue.js Tutorial"
The above is the detailed content of A brief analysis of several ways to create components in Vue. For more information, please follow other related articles on the PHP Chinese website!

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

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

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

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),