Home Web Front-end JS Tutorial Detailed analysis of use cases of vue global components and local components

Detailed analysis of use cases of vue global components and local components

May 02, 2018 pm 05:21 PM
use local components

This time I will bring you a detailed analysis of the use cases of vue global components and local components. What are the precautions when using vue global components and local components use cases. The following is a practical case. Let’s take a look. .

main.jsSome common configurations of the entry file, the public.vue defined on the entry file is a global component, and the pug template .wrapper used here is equivalent to< p class=wrapper>

main.jsFile

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

**main.js入口文件的内容**

import Vue from 'vue'

import App from './App'

import router from './router'

// 引入公用组件的vue文件 他暴漏的是一个对象

import cpublic from './components/public'

Vue.config.productionTip = false

// 注册全局组件-要在vue的根事咧之前

// 参数 1是标签名字-string 2是对象 引入外部vue文件就相当与一个对象

Vue.component('public', cpublic)

// 正常注册全局组件的时候,第二个参数应该是对象。

Vue.component('public1', {

 template: '<p>正常的组件模式</p>'

})

/* eslint-disable no-new */

// 生成vue 的根实例;创建每个组件都会生成一个vue的事咧

new Vue({

 el: '#app',

 router,

 template: '<App/>',

 components: { App }

})

Copy after login

public.vue Component is a defined global component It can be used directly in any component. There is no need to define it again on the vue instance option components, and there is no need to import the file path again.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

**public.vue的组件内容**

<template lang="pug">

.wrapper

 slot(text="我是全局组件") {{name}}

</template>

<script>

export default {

 name: 'HelloWor',

 // 全局组件里data属性必须是函数,这样才会独立,

 // 在组件改变状态的时候不会影响其他组件里公用的这个状态

 data () {

  return {

   name: '我是全局组件'

  }

 }

}

</script>

<style scoped>

</style>

Copy after login

In the parent.vue component, public global components and other sub-components are used

parent.vue component

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

<template lang="pug">

.wrap

 .input-hd

  .title 名称:

  input.input(type="text",v-model="msg",placeholder="请输入正确的值",style="outline:none;")

 .content-detail

  .content-name 我是父组件的内容 

 children(:msg='msg', number='1')

 public

 router-link(to='/parent/children2') 第二个子组件

 router-view

</template>

<script>

import children from './children'

// children(:msg='msg', number='1')在组件里 也可以传递自定义的属性,但是是字符串类型,

export default {

 name: 'HelloWor',

 data () {

  return {

   // 通过prop将数据传递到子组件,并与v-model想对应的输入框相互绑定。

   msg: '这个是父组件的-prop-数据'

  }

 },

 components: {

  children

 }

}

</script>

<style scoped>

.wrap {

}

.input-hd {

 display: flex;

 flex-direction: row;

 align-items: center;

 height: 70px;

}

</style>

Copy after login

children.vue is a subcomponent of parent.vue, but it is only available in the scope of parent.vue

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<template lang="pug">

.wrapper

 slot(text="我是子组件的text") 我是子组件的内容

 .name {{ msg }} {{ number }}

</template>

<script>

export default {

 name: 'HelloWor',

 // 接受的时候是用props接受,数组的形式,里面是字符串的形式。

 // 也可以传入普通的字符串

 // 在子组件中,props接受到的状态当作vue的实例属性来使用

 props: [ 'msg''number' ]

}

</script>

<style scoped>

</style>

Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to php Chinese website Other related articles!

Recommended reading:

How to use the diff algorithm

JS EventEmitter event specific usage details

The above is the detailed content of Detailed analysis of use cases of vue global components and local components. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use magnet links How to use magnet links Feb 18, 2024 am 10:02 AM

How to use magnet links

How to use mdf and mds files How to use mdf and mds files Feb 19, 2024 pm 05:36 PM

How to use mdf and mds files

What software is crystaldiskmark? -How to use crystaldiskmark? What software is crystaldiskmark? -How to use crystaldiskmark? Mar 18, 2024 pm 02:58 PM

What software is crystaldiskmark? -How to use crystaldiskmark?

How to install the Windows 10 old version component DirectPlay How to install the Windows 10 old version component DirectPlay Dec 28, 2023 pm 03:43 PM

How to install the Windows 10 old version component DirectPlay

How to download foobar2000? -How to use foobar2000 How to download foobar2000? -How to use foobar2000 Mar 18, 2024 am 10:58 AM

How to download foobar2000? -How to use foobar2000

How to use NetEase Mailbox Master How to use NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

How to use NetEase Mailbox Master

How to use Xiaoai Speaker How to connect Xiaoai Speaker to mobile phone How to use Xiaoai Speaker How to connect Xiaoai Speaker to mobile phone Feb 22, 2024 pm 05:19 PM

How to use Xiaoai Speaker How to connect Xiaoai Speaker to mobile phone

How to use Baidu Netdisk app How to use Baidu Netdisk app Mar 27, 2024 pm 06:46 PM

How to use Baidu Netdisk app

See all articles