Home >Web Front-end >JS Tutorial >Summary of vue global components

Summary of vue global components

php中世界最好的语言
php中世界最好的语言Original
2018-06-08 14:01:001519browse

This time I will bring you a summary of vue global components. What are the precautions for the summary of vue global components? The following is a practical case, let's take a look.

Data-driven and componentized 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.

1. Global component

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>Vue.component() must be reused each time a component is defined. And the component names cannot be the same;

Example:

Welcome component

2, local 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

3. Use the template tag

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

4. Single file 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:

vue2.0 operation active and other options are mutually exclusive

Why can't data be saved after the vuex page is refreshed? deal with

The above is the detailed content of Summary of vue global components. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn