Home >Web Front-end >JS Tutorial >Specification for writing vue components
This time I will bring you the vue component writing specification. What are the precautions of the vue component writing specification. The following is a practical case, let's take a look.
DataDriven and componentization are the two most important features of vue.js. Componentization is to facilitate code reuse and improve development efficiency. There are four common ways to write Vue components, each with its own characteristics and suitable for different scenarios.
Structure:
// 组件的注册 Vue.component( 'componentName', { template: // 组件的html结构, data(){ return{ // 组件中的属性 } }, method: { // 组件中的方法 } ...... // 组件其他的属性和方法 }) // 组件的使用 new Vue({ el: '#app' })
Define a global component through Vue.component() in the script tag component, and apply the component to the tag with the id app in the html file through the new Vue() instance.
Features:
<1> Can be directly defined and used in the script tag in the html file;
<2> Pass The components defined by this method are global components and can be used in any Vue instance. They are suitable for relatively simple project scenarios;
<3> They must be reused every time defining a component Vue.component(), and the component names cannot be the same;
Example:
Welcome component
Structure:
// 构造组件对象 const componentName = { template: // 组件的html结构, data(){ return{ // 组件中的属性 } }, method: { // 组件中的方法 } ...... // 组件其他的属性和方法 } // 组件的使用 new Vue({ el: '#app', components: { // 组件注册、调用 componentName } })
Define a component object in the script tag and register the component through the components attribute in the Vue instance.
Features:
<1>Similar to components defined globally, components can be written and used directly in the script tag in the html file;
<2>This component can only be used in a registered Vue instance;
Instance:
Welcome component
Structure:
<template id="componnet"> // 组件的html结构 </template> // 全局组件的注册与使用 Vue.component( 'componentName', { template: '#component', data(){ return{ // 组件中的属性 } }, method: { // 组件中的方法 } ...... // 组件其他的属性和方法 }) new Vue({ el: '#app' }) // 局部组件的注册与使用 const componentName = { template: '#component', data(){ return{ // 组件中的属性 } }, method: { // 组件中的方法 } ...... // 组件其他的属性和方法 } new Vue({ el: '#app', components: { // 组件注册、调用 componentName } })
Use the template tag to write the html structure in the component inside the body tag and in the script tag It is registered and used in the way of global components and local components. The difference is that the template attribute in the component is referenced by id.
Features:
<1>The js file does not contain html structure content, realizing the separation of structure and logic;
Example:
Welcome component
Structure:
<template lang="html"> // 组件中的html结构 </template> <script> //组件的逻辑 export default { // 组件的属性和方法 } </script> <style lang="css" scoped> // 组件的样式 </style>
Creation A file with the suffix vue, the file name is the component name. The component contains three parts: html structure, js logic, and css style, which correspond to different tags. When using components, you can use them by importing them.
Features:
<1>Components do not affect each other and have high reusability, and their html, css, and js can all be reused;
<2>The structure and logic of the components are clear;
<3>Suitable for large and complex projects, suitable for multi-person development;
Example:
Welcome component
! ! ! It should be noted that: all tags in the template tag must be wrapped with one tag, otherwise an error will be reported
Correct way of writing:
<template> <p> <p></p> ...... <p></p> </p> </template>
Wrong way of writing:
<template> <p></p> <p></p> ...... <p></p> </template>
Believe it After reading the case in this article, you have mastered the method. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Recommended reading:
Detailed explanation of using vuex
##What are the methods to operate render execution
The above is the detailed content of Specification for writing vue components. For more information, please follow other related articles on the PHP Chinese website!