欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue中組件的3種使用方式詳解

 更新時間:2019年03月23日 11:45:37   作者:貴飛  
這篇文章主要給大家介紹了關于vue中組件的3種使用方式,文中通過示例代碼介紹的非常詳細,對大家學習或者使用vue具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧

前言

組件是Vue.js最強大的功能之一。組件可以擴展HTML元素,封裝可重用的代碼。

在vue angular react三大前端框架的大前端時代。許多人選擇了vue,在 github 上的star,vue已經(jīng)超過react的數(shù)量了。雖然star并不能代表vue更強,不過在發(fā)展速度上看來,vue確實很快。


在模塊化的前端時代,萬物皆組件,vue學習組件是必不可少的。

可是在大多數(shù)人熟悉了純html、jq之后,在初次接觸vue的組件時候,卻是滿臉蒙蔽。
今天咱們以最簡單的方式,帶vue小白童鞋們,步入組件的世界~

咱們今天講三種組件使用方式

  • 基本組件
  • 全局組件
  • 構造組件

1. 基本組件四步驟

  • 寫好組件(廢話~)
  • 在頁面種引用組件
  • 在components中聲明組件
  • 在頁面上使用

咱們以一個button子組件為例

項目src結構:

組件一般都放在components文件夾下:

1.寫好子組件:

<template>
 <button class="btn" :style="{color:color}">
 <slot/> <!-- 插槽 -->
 </button>
</template>

<script>
export default {
 // 傳入子組件的參數(shù)寫到props
 props: {
 color: {
 type: String, // 顏色參數(shù)類型
 default: "#000" // 默認黑色
 }
 }
}
</script>

<style scoped>
 .btn {
 width: 110px;
 height: 60px;
 border-radius: 10px;
 border: none;
 font-size: 15px;
 }
</style>

2.3.4.父組件:

<template>
 <div id="app">
 <!-- 4. 在頁面上使用 -->
 <Button color="red">我是插槽的值</Button>
 </div>
</template>

<script>
// 2. 在頁面種引用組件
import Button from '@/components/Button.vue'
export default {
 name: "app",
 // 3. 在components中聲明組件
 components: {
 Button
 }
};
</script>

效果:

2. 全局組件五步驟

  • 寫好組件(還是廢話~)
  • 子組件添加install方法
  • 在 main.js 中引用
  • 使用 Vue.use 方法
  • 在頁面上使用

1.子組件還是那樣~~:

2. 子組件添加install方法

Button.js :

import ButtonComponent from './Button.vue'

// 添加install方法 (插件方法)
const Button = {
 install: function (Vue) {
 Vue.component("Button", ButtonComponent);
 }
}

// 導出Button
export default Button

當然 你可以處理多個全局組件:

import ButtonComponent1 from './Button1.vue'
import ButtonComponent2 from './Button2.vue'
import ButtonComponent3 from './Button3.vue'

const buttonList = [
 ButtonComponent1,
 ButtonComponent2,
 ButtonComponent3
];
// 添加install方法 (插件方法)
const Button = {
 install: function (Vue) {
 buttonList.forEach(button=>{
 // 這里 使用每個組件的 name 屬性作為組件名
 Vue.component(button.name, button);
 })
 }
}

// 導出Button
export default Button

3.4. main.js

import Vue from 'vue'
import App from './App.vue'
// 3
import Button from '@/components/Button.js'
// 4
Vue.use(Button);
new Vue({
 render: h => h(App),
}).$mount('#app')

5. 在頁面上使用
app.vue:

<template>
 <div id="app">
 <!-- 5. 在頁面上使用 -->
 <Button color="blue">我是全局按鈕</Button>
 </div>
</template>

效果如下:

2. 構造組件四步驟

  • 寫好組件(還**是廢話~)
  • vue.extend構建組件
  • 掛載 Vue.prototype
  • 在js中使用

1.寫好子組件:

<template>
 <p class="Message">{{value}}</p>
</template>

<script>
export default {
 data() {
 return {
  value: "我是一個彈框"
 };
 }
};
</script>

<style>
.Message {
 position: fixed;
 bottom: 30px;
 width: 200px;
 background-color: rgba(0, 0, 0, 0.5);
 color: #fff;
 border-radius: 10px;
 left: 50%;
 transform: translateX(-50%);
 line-height: 30px;
 text-align: center;
 font-size: 15px;
 animation: messageFade 3s 1;
}
/* 加個簡單動畫 */
@keyframes messageFade {
 0% {
 opacity: 0;
 -webkit-transform: translate3d(-50%, 80%, 0);
 transform: translate3d(-50%, 80%, 0);
 }
 16% {
 opacity: 1;
 -webkit-transform: translate3d(-50%, 0, 0);
 transform: translate3d(-50%, 0, 0);
 }
 84% {
 opacity: 1;
 -webkit-transform: translate3d(-50%, 0, 0);
 transform: translate3d(-50%, 0, 0);
 }
 100% {
 opacity: 0;
 -webkit-transform: translate3d(-50%, 80%, 0);
 transform: translate3d(-50%, 80%, 0);
 }
}
</style>

2. vue.extend構建組件

Message.js :

import Vue from 'vue';
import Message from './Message.vue';
// 構造組件
const MessageConstructor = Vue.extend(Message);
// 設置刪除組件
const removeDom = (target) => {
 target.parentNode.removeChild(target);
};
// 構造組件添加關閉方法
MessageConstructor.prototype.close = function() {
 this.visible = false;
 removeDom(this.$el);
};

const MessageDiv = (options) => {
 // 實例化組件
 const instance = new MessageConstructor({
  el: document.createElement('div'),
  // 組件參數(shù),運用到組件內(nèi)的data
  data: options,
 });
 // 在body添加組件
 document.body.appendChild(instance.$el);
 Vue.nextTick(() => {
  instance.timer = setTimeout(() => {
   // 定時關閉組件
   instance.close();
  }, 3000);
 });
 return instance;
};

export default MessageDiv;

3. 掛載 Vue.prototype

main.js :

import Message from '@/components/Message.js'
Vue.prototype.$message = Message;

4. 使用:

<template>
 <div id="app">
 <Button color="blue" @click.native="msg">我是全局按鈕</Button>
 </div>
</template>

<script>
import Button from "@/components/Button.vue";
export default {
 name: "app",
 components: {
 Button
 },
 methods: {
 msg() {
  // 4. 使用構造組件
  this.$message({value:'我是構造組件'});
 }
 }
};
</script>

效果:

以上就是三種組件的基本使用啦~~

總結

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。

相關文章

  • elementui+vue+axios實現(xiàn)文件上傳本地服務器

    elementui+vue+axios實現(xiàn)文件上傳本地服務器

    這篇文章主要為大家詳細介紹了elementui+vue+axios實現(xiàn)文件上傳本地服務器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • 詳談vue中的rules表單驗證(常用)

    詳談vue中的rules表單驗證(常用)

    這篇文章主要介紹了關于vue中的rules表單驗證(常用),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • electron-vue+electron-updater實現(xiàn)自動更新(步驟源碼)

    electron-vue+electron-updater實現(xiàn)自動更新(步驟源碼)

    這篇文章主要介紹了electron-vue+electron-updater實現(xiàn)自動更新,步驟源碼包括autoUpdater.js操控更新js文件,main.js也就是package.json內(nèi)的main指向的js文件,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-10-10
  • Element-Plus?el-col、el-row快速布局及使用方法

    Element-Plus?el-col、el-row快速布局及使用方法

    這篇文章主要介紹了Element-Plus?el-col、el-row快速布局及使用方法,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-12-12
  • 圖文詳解Vue3沒有代碼提示問題的解決辦法

    圖文詳解Vue3沒有代碼提示問題的解決辦法

    最近在使用Vue.js時候沒有自動提示,就很難受,下面這篇文章主要給大家介紹了關于Vue3沒有代碼提示問題的解決辦法,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2023-01-01
  • vue中循環(huán)多個li(表格)并獲取對應的ref的操作代碼

    vue中循環(huán)多個li(表格)并獲取對應的ref的操作代碼

    我想要獲取每一個循環(huán)并獲取每一個li(或者其它循環(huán)項)的ref,以便于后續(xù)的操作,接下來通過本文給大家分享vue中循環(huán)多個li(表格)并獲取對應的ref的操作代碼,感興趣的朋友跟隨小編一起看看吧
    2024-02-02
  • 基于vue中keep-alive緩存問題的解決方法

    基于vue中keep-alive緩存問題的解決方法

    今天小編就為大家分享一篇基于vue中keep-alive緩存問題的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • 使用vue實現(xiàn)HTML頁面生成圖片的方法

    使用vue實現(xiàn)HTML頁面生成圖片的方法

    這篇文章主要介紹了使用vue實現(xiàn)HTML頁面生成圖片的相關知識,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • Vue使用mockjs問題(返回數(shù)據(jù)、get、post 請求)

    Vue使用mockjs問題(返回數(shù)據(jù)、get、post 請求)

    這篇文章主要介紹了Vue使用mockjs問題(返回數(shù)據(jù)、get、post 請求),具有很好的參考價值,希望對大家有所幫助。
    2023-05-05
  • vue favicon設置以及動態(tài)修改favicon的方法

    vue favicon設置以及動態(tài)修改favicon的方法

    這篇文章主要介紹了vue favicon設置以及動態(tài)修改favicon的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12

最新評論