Vue:如何在vue-cli中創建並引入自定義組件


一、創建並引入一個組件

1、創建組件

vue-cli中的所有組件都是存放在components文件夾下面的,所以在components文件夾下面創建一個名為First.vue的自定義組件:

<template>
    <div>
        <h1>{{msg}}</h1>
    </div>
</template>
<script>

// 1、export 表示導出,在自定義組件里面使用export default導出  
export default {
    // name 表示設置別名,可以不設置,建議和組件的名稱一致
    name:"First",
    data(){
        return{
            msg:"First Vue"
        }
    }
}
</script>

2、在App.vue組件里面引用First.vue組件

1、在<script>標簽里面使用import導入自定義的標簽:

// 1、導入自定義組件 First即First.vue組件里面設置的name值
import First from "./components/First"

2、在export里面添加自定義組件:

 // 2、添加自定義組件
  components:{
    First
  }

 3、在<template>標簽里面引入自定義組件:

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <!-- <router-view/> -->
    <!--3、引用自定義組件-->
    <First></First>
  </div>
</template>

 完整代碼如下:

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <!-- <router-view/> -->
    <!--3、引用自定義組件-->
    <First></First>
  </div>
</template>

<script>
// 1、導入自定義組件 First即First.vue組件里面設置的name值
import First from "./components/First"
export default {
  name: 'App',
  // 2、添加自定義組件
  components:{
    First
  }
}
</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>

 效果:

二、引入嵌套組件

在上面的示例中,只是在App.vue中引入了單一的組件,如何引入嵌套組件呢?即First.vue組件里面又引用了自定義組件,這時該如何在App.vue組件里面引入呢?

1、先定義Second.vue自定義組件:

<template>
    <div>
        <h1>{{secondMsg}}</h1>
    </div>
</template>

<script>
export default {
    // name 表示設置別名,可以不設置,建議和組件的名稱一致
    name :"Second",
    data(){
       return{
           secondMsg:"Second vue"
       }
    }
}
</script>

2、在First.vue組件中引用Second.vue組件

在First.vue中引用Second.vue組件和在App.vue中引入First.vue組件是一樣的,完整代碼如下:

<template>
    <div>
        <h1>{{msg}}</h1>
        <!--3、引用second.vue組件-->
        <Second></Second>
    </div>
</template>
<script>
// 1、使用import導入Second.vue
import Second from './Second';
// export 表示導出
export default {
    // name 表示設置別名,可以不設置,建議和組件的名稱一致
    name:"First",
    data(){
        return{
            msg:"First Vue"
        }
    },
    // 2、添加自定義組件組件
    components:{
        Second
    }
}
</script>

3、在App.vue中引入嵌套組件

First.vue中引入了Second.vue組件以后,可以把First.vue組件看成是一個組件了,所以在App.vue中引入的時候代碼是一樣的:

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <!-- <router-view/> -->
    <!--3、引用自定義組件-->
    <First></First>
  </div>
</template>

<script>
// 1、導入自定義組件 First即First.vue組件里面設置的name值
import First from "./components/First"
export default {
  name: 'App',
  // 2、添加自定義組件
  components:{
    First
  }
}
</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>

4、效果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM