亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

解析Vue.js中的組件

 更新時間:2018年02月02日 08:33:46   作者:justme  
組件(Component)是 Vue.js 最強大的功能之一。組件可以擴展 HTML 元素,封裝可重用的代碼。這篇文章主要介紹了vue.js 中的組件,需要的朋友參考下

介紹

在使用Vue.js時,Vue.js組件非常重要。在本教程中,我們將深入研究Vue.js組件,理解基礎知識并將其應用于應用程序。讓我們開始吧。 

什么是組件?

組件使我們能夠?qū)?復雜的 應用程序分解成小塊。例如,典型的Web應用程序?qū)⒕哂袠祟},側(cè)邊欄,內(nèi)容和頁腳等部分。

Vue.js允許我們將每個部分分解成單獨的模塊化代碼,稱為組件。這些組件可以擴展,然后附加到 你 正在處理的應用程序。 使用 組件是 在 整個應用程序 編寫 中重用代碼的好方法。

假設 你 有一個博客應用程序,并且 你 想要顯示 一列 博客 帖子 。使用Vue組件,你可以做:

<blog-post></blog-post>

Vue處理剩下的事情。

創(chuàng)建一個將Vue實例掛載到DOM元素的簡單HTML頁面。 你 將使用它來了解組件。以下是HTML頁面 樣例 :

<!DOCTYPE html>
<html>
<head>
<title>VueJs Components</title>
</head>
<body>
 <!-- Div where Vue instance will be mounted -->
 <div id="app"></div>
 
 <!-- Vue.js is included in your page via CDN -->
 <script src="https://unpkg.com/vue"></script>
 <script>
  // A new Vue instance is created and mounted to your div element
  new Vue({
   el: '#app',
   data: {
   domain: 'Tutsplus'
   },
   template: '<p>Welcome to {{ domain }}</p>
  });
 </script>
</body>
</html>

在上面,你創(chuàng)建了一個簡單的Vue實例,在代碼中沒有組件因素。 現(xiàn)在,如果 你 希望歡迎消息出現(xiàn)兩次,那么 你 怎么做?

你的猜測可能是 讓 div   在 Vue實例掛載的地方出現(xiàn)兩次。 這是行不通的。 嘗試改變它從 id 到 class , 你會得到 :

<!DOCTYPE html>
<html>
<head>
<title>VueJs Components</title>
</head>
<body>
 <!-- Div where Vue instance will be mounted -->
 <div class="app"></div>
 <div class="app"></div>
 
 <!-- Vue.js is included in your page via CDN -->
 <script src="https://unpkg.com/vue"></script>
 <script>
  // A new Vue instance is created and mounted to your div element
  new Vue({
   el: '.app',
   data: {
   domain: 'Tutsplus'
   },
   template: '<p>Welcome to {{ domain }}</p>
  });
 </script>
</body>
</html>

它仍然不會工作!

解決這個問題的唯一方法是創(chuàng)建一個組件。 你如何創(chuàng)建一個組件?

組件是使用Vue.component()構(gòu)造函數(shù)創(chuàng)建的。 這個構(gòu)造函數(shù)有兩個參數(shù):你的組件的名字(也可以叫做標簽名)和一個包含組件選項(options)的對象。

讓我們使用上面的內(nèi)容創(chuàng)建一個組件。

Vue.component('welcome-message', {
  data: function() {
  return {
   domain: 'Tutsplus'
  }
  },
  template: '<p>Welcome to {{ domain }}</p>'
 })

在上面,組件名稱被稱為welcome-message。 你的組件可以有你選擇的任何名稱。 然而重要的是,這個名字不會影響任何HTML標簽,因為你不想重寫它。

傳遞給構(gòu)造函數(shù)的options對象包含數(shù)據(jù)和模板。 在創(chuàng)建組件時,你的數(shù)據(jù)應該是一個函數(shù),如上所見。 被保存的數(shù)據(jù)應該作為一個對象返回。

在沒有數(shù)據(jù)可以傳遞的情況下,傳遞如下的模板:

 Vue.component('welcome-message', {
  template: '<p>Welcome to Tutsplus</p>'
 })

完成之后,可以通過使用傳遞給構(gòu)造函數(shù)的名稱將其當作常規(guī)HTML元素來在應用程序中使用組件。 它被這樣調(diào)用:<welcome-message> </ welcome-message>

要多次輸出模板,可以根據(jù)需要多次調(diào)用組件,如下所示。

<!DOCTYPE html>
<html>
<head>
<title>VueJs Components</title>
</head>
<body>
 <!-- Div where Vue instance will be mounted -->
 <div id="app">
 <welcome-message></welcome-message>
 <welcome-message></welcome-message>
 <welcome-message></welcome-message>
 <welcome-message></welcome-message>
 </div>
 
 <!-- Vue.js is included in your page via CDN -->
 <script src="https://unpkg.com/vue"></script>
 <script>
 Vue.component('welcome-message', {
  data: function() {
  return {
   domain: 'Tutsplus'
  }
  },
  template: '<p>Welcome to {{ domain }}</p>'
 })
 
 // A new Vue instance is created and mounted to your div element
 new Vue({
  el: '#app'
 });
 </script>
</body>
</html>

這樣一來,歡迎消息將顯示四次。

將數(shù)據(jù)存儲在組件中

上面我提到數(shù)據(jù)必須是一個函數(shù),它所包含的信息必須作為一個對象返回。 為什么這樣?

當返回的數(shù)據(jù)不是對象時,使用該數(shù)據(jù)的組件共享相同的源:共享數(shù)據(jù)。 因此,一個組件的數(shù)據(jù)變化會影響另一個組件。 當數(shù)據(jù)作為對象返回時,這是不一樣的。

看看這是如何實際工作是很重要的。 首先,讓我們看看數(shù)據(jù)作為對象返回的情況。

<!DOCTYPE html>
<html>
<head>
<title>VueJs Components</title>
</head>
<body>
 <!-- Div where Vue instance will be mounted -->
 <div id="app">
 <welcome-message></welcome-message>
 <welcome-message></welcome-message>
 </div>
 
 <!-- Vue.js is included in your page via CDN -->
 <script src="https://unpkg.com/vue"></script>
 <script>
 var data = { name: 'Henry' }
 
 Vue.component('welcome-message', {
  data: function() {
  return data
  },
  template: '<p>Hello {{ name }}, welcome to TutsPlus (<button @click="changeName">Change Name</button>)</p>',
  methods :{
   changeName: function() {
   this.name = 'Mark'
   }
  }
 })
 
 // A new Vue instance is created and mounted to your div element
 new Vue({
  el: '#app'
 });
 </script>
</body>
</html>

你能猜到上面發(fā)生了什么嗎?

有兩個組件,并且這兩個組件共享相同的數(shù)據(jù)源,因為數(shù)據(jù)不作為對象返回。 你怎么證明我是對的? 當從瀏覽器查看上述頁面時,你將看到一個組件的更改會導致另一個組件的數(shù)據(jù)發(fā)生更改。那么它應該是怎樣的呢?

像這樣:

<!DOCTYPE html>
<html>
<head>
<title>VueJs Components</title>
</head>
<body>
 <!-- Div where Vue instance will be mounted -->
 <div id="app">
 <welcome-message></welcome-message>
 <welcome-message></welcome-message>
 </div>
 
 <!-- Vue.js is included in your page via CDN -->
 <script src="https://unpkg.com/vue"></script>
 <script>
 
 Vue.component('welcome-message', {
  data: function() {
  return {
   name: 'Henry'
  }
  },
  template: '<p>Hello {{ name }}, welcome to TutsPlus (<button @click="changeName">Change Name</button>)</p>',
  methods :{
   changeName: function() {
   this.name = 'Mark'
   }
  }
 })
 
 // A new Vue instance is created and mounted to your div element
 new Vue({
  el: '#app'
 });
 </script>
</body>
</html>

這里的數(shù)據(jù)是作為一個對象返回的,一個組件的改變不會影響另一個組件。 該功能是針對單個組件執(zhí)行的。 在構(gòu)建應用程序時,不要忘記這一點,這很重要。

創(chuàng)建和使用組件

使用到目前為止學到的內(nèi)容,讓我們使用 vue -cli 從頭開始一個新的Vue.js項目來實現(xiàn)它。 如果你的機器上沒有安裝 vue -cli ,你可以通過運行:

npm install -g vue-cli

開始你的新的Vue.js項目:

vue init webpack vue-component-app

導航到你的應用程序,安裝依賴關(guān)系,并使用下面的命令運行你的開發(fā)服務器。

cd vue-component-app
npm install
npm run dev

首先,你將重命名HelloWorld組件,這個組件是你將應用程序初始化為Hello.vue時創(chuàng)建的組件。然后你將注冊這個組件作為一個全局組件在你的應用程序中使用。

所以你的Hello組件應該看起來像這樣。

#src/components/Hello.vue
<template>
 <div class="hello">
 <p>Welcome to TutsPlus {{ name }}</p>
 <hr>
 <button @click="changeName">Change Display Name</button>
 </div>
</template>
<script>
export default {
 name: 'Hello',
 data () {
 return {
  name: 'Henry'
 }
 },
 methods: {
 changeName () {
  this.name = 'Mark'
 }
 }
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
 font-weight: normal;
}
ul {
 list-style-type: none;
 padding: 0;
}
li {
 display: inline-block;
 margin: 0 10px;
}
a {
 color: #42b983;
}
</style>

你有歡迎文本顯示歡迎消息和作為數(shù)據(jù)傳遞的名稱。 當點擊歡迎消息下方的按鈕時,將調(diào)用changeName方法。 名字將從亨利改為馬克。

要全局使用此組件,必須被注冊。你能猜到應該在哪里完成這個操作嗎?如果你說main.js,恭喜你,答對了!

要注冊一個組件,可以導入它,然后使用Vue.component()構(gòu)造函數(shù)進行設置。自己動手試試。

我敢打賭,這個對你來說小菜一碟。以下是main.js文件中的內(nèi)容。

#src/main.js
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import Home from './components/Hello'
Vue.config.productionTip = false
Vue.component('display-name', Home)
/* eslint-disable no-new */
new Vue({
 el: '#app',
 template: '<App/>',
 components: { App }
})

這里除了導入你的Hello組件的那一行之外,沒有什么新東西。然后使用構(gòu)造函數(shù)注冊該組件。最后,對于這部分,組件需要使用你輸入的組件名稱來顯示。在這種情況下,組件是顯示名稱。這將在你的App.vue文件中完成。

打開src / App.vue并使其看起來像這樣。

#src/App.vue
<template>
<div id= "app" >
<display-name/>
</div>
</template>
<script>
export default {
}
</script>
<style>
#app {
font-family: 'Avenir' , Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>

打開服務器,打開http:// localhost:8080。 點擊按鈕,名稱應該改變。

我們來看看如何在本地使用一個組件。

在組件目錄中創(chuàng)建一個名為Detail.vue的文件。 這個組件不會做任何特殊的事情 - 它將被用在Hello組件中。

使你的Detail.vue文件就像這樣。

#src/components/Detail.vue
<template>
 <div class="hello">
 <p>This component is imported locally.</p>
 </div>
</template>
<script>
export default {
 name: 'Detail'
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
 font-weight: normal;
}
ul {
 list-style-type: none;
 padding: 0;
}
li {
 display: inline-block;
 margin: 0 10px;
}
a {
 color: #42b983;
}
</style>

要在Hello組件中使用它,可以像導入Hello組件一樣將其導入。 接下來,把它注冊,但這次你不需要使用Vue.component()構(gòu)造函數(shù)。

你可以使用導出內(nèi)的組件對象進行注冊。將用作元素標記的組件的名稱必須作為值傳遞給對象。 完成后,你現(xiàn)在可以使用元素標記來輸出組件。

為了理解這一點,Hello組件應該長這樣:

#src/components/Hello.vue
<template>
 <div class="hello">
 <p>Welcome to TutsPlus {{ name }}</p>
 <hr>
 <button @click="changeName">Change Display Name</button>
 <!-- Detail component is outputted using the name it was registered with -->
 <Detail/>
 </div>
</template>
<script>
// Importation of Detail component is done
import Detail from './Detail'
export default {
 name: 'HelloWorld',
 data () {
 return {
  name: 'Henry'
 }
 },
 methods: {
 changeName () {
  this.name = 'Mark'
 }
 },
 /**
 * Detail component gets registered locally.
 * This component can only be used inside the Hello component
 * The value passed is the name of the component
 */
 components: {
 Detail
 }
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
 font-weight: normal;
}
ul {
 list-style-type: none;
 padding: 0;
}
li {
 display: inline-block;
 margin: 0 10px;
}
a {
 color: #42b983;
}
</style>

刷新頁面以查看新頁面。

范圍組件樣式

Vue.js允許你為組件提供全局和本地樣式。是什么意思呢?在某些情況下,你希望組件中的某些元素與另一個組件中的對應元素的樣式不同。Vue.js能夠幫助你。

一個很好的例子是你剛剛建立的小應用程序。App.vue中的樣式是全局的; 這怎么可能? 打開你的App.vue,風格部分看起來像這樣。

<style>
#app {
 font-family: 'Avenir', Helvetica, Arial, sans-serif;
 -webkit-font-smoothing: antialiased;
 -moz-osx-font-smoothing: grayscale;
 text-align: center;
 color: #2c3e50;
 margin-top: 60px;
}
</style>

這與Detail.vue不同,看起來像這樣。

<style scoped>
h1, h2 {
 font-weight: normal;
}
 
ul {
 list-style-type: none;
 padding: 0;
}
 
li {
 display: inline-block;
 margin: 0 10px;
}
 
a {
 color: #42b983;
}
</style>

將   scoped 添加到樣式標簽是造成這個差別的原因。 嘗試通過刪除   scoped 來編輯一種組件樣式,你會看到這是如何運作的。

結(jié)論

雖然這個文章有點長,但是我相信你會喜歡它。

現(xiàn)在你知道如何有效地使用組件,并且了解如何在現(xiàn)有應用程序中構(gòu)建組件。在使用vue-cli時,你還可以在本地和全局范圍內(nèi)創(chuàng)建和使用組件。當你想為一個組件使用特定的風格時,你已經(jīng)看到了如何使用scoped來做到這一點。

你現(xiàn)在可以繼續(xù)構(gòu)建使用組件的復雜Vue.js應用程序。了解Vue.js允許你重用代碼,你的頁眉,頁腳,登錄面板和搜索欄可以用作組件。

總結(jié)

以上所述是小編給大家介紹的Vue.js中的組件,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Vue中使用better-scroll實現(xiàn)輪播圖組件

    Vue中使用better-scroll實現(xiàn)輪播圖組件

    better-scroll 是一款重點解決移動端(已支持 PC)各種滾動場景需求的插件。這篇文章主要介紹了Vue中使用better-scroll實現(xiàn)輪播圖組件的實例代碼,需要的朋友可以參考下
    2020-03-03
  • Vue2項目升級到Vue3的詳細教程

    Vue2項目升級到Vue3的詳細教程

    看到好多開源項目都升級了vue3,下面這篇文章主要給大家介紹了關(guān)于Vue2項目升級到Vue3的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-01-01
  • Vue項目三級聯(lián)動路由跳轉(zhuǎn)與傳參的思路詳解

    Vue項目三級聯(lián)動路由跳轉(zhuǎn)與傳參的思路詳解

    這篇文章主要介紹了Vue項目三級聯(lián)動的路由跳轉(zhuǎn)與傳參的思路詳解,本文給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2024-08-08
  • vue中axios的封裝問題(簡易版攔截,get,post)

    vue中axios的封裝問題(簡易版攔截,get,post)

    這篇文章主要介紹了vue中axios的封裝問題(簡易版攔截,get,post),需要的朋友可以參考下
    2018-06-06
  • Vue3中ref的用法舉例總結(jié)(避免混淆)

    Vue3中ref的用法舉例總結(jié)(避免混淆)

    這篇文章主要給大家介紹了關(guān)于Vue3中ref的用法舉例總結(jié)的相關(guān)資料,ref()接受一個內(nèi)部值,返回一個響應式的可更改的ref對象,此對象只有一個指向其內(nèi)部值的屬性.value,需要的朋友可以參考下
    2023-10-10
  • 淺析從vue源碼看觀察者模式

    淺析從vue源碼看觀察者模式

    本篇文章主要介紹了vue源碼看觀察者模式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • vue3引入ElementUI報錯問題及解決

    vue3引入ElementUI報錯問題及解決

    這篇文章主要介紹了vue3引入ElementUI報錯問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Vue中進行分布式鑒權(quán)與認證的過程

    Vue中進行分布式鑒權(quán)與認證的過程

    在Vue應用中,我們通常需要實現(xiàn)分布式鑒權(quán)和認證,以確保用戶的安全性和數(shù)據(jù)的保密性,本文將介紹在Vue中如何進行分布式鑒權(quán)與認證,需要的朋友可以參考下
    2023-06-06
  • Vite?Vue3?EsLint?Prettier配置步驟極簡方法詳解

    Vite?Vue3?EsLint?Prettier配置步驟極簡方法詳解

    這篇文章主要為大家介紹了Vite?Vue3?EsLint?Prettier配置步驟的極簡方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • Element-ui設置el-table表頭全選框隱藏或禁用

    Element-ui設置el-table表頭全選框隱藏或禁用

    這篇文章主要給大家介紹了關(guān)于Element-ui設置el-table表頭全選框隱藏或禁用的相關(guān)資料,文中手把手教你實現(xiàn)el-table實現(xiàn)跨表格禁用選項,需要的朋友可以參考下
    2023-07-07

最新評論