3種vue組件的書寫形式
本文實(shí)例為大家分享了vue組件的書寫形式,供大家參考,具體內(nèi)容如下
第一種使用script標(biāo)簽
<!DOCTYPE html>
<html>
<body>
<div id="app">
<my-component></my-component>
</div>
<-- 注意:使用<script>標(biāo)簽時(shí),type指定為text/x-template,意在告訴瀏覽器這不是一段js腳本,瀏覽器在解析HTML文檔時(shí)會(huì)忽略<script>標(biāo)簽內(nèi)定義的內(nèi)容。-->
<script type="text/x-template" id="myComponent">//注意 type 和id。
<div>This is a component!</div>
</script>
</body>
<script src="js/vue.js"></script>
<script>
//全局注冊(cè)組件
Vue.component('my-component',{
template: '#myComponent'
})
new Vue({
el: '#app'
})
</script>
</html>
第二種使用template標(biāo)簽
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="app">
<my-component></my-component>
</div>
<template id="myComponent">
<div>This is a component!</div>
</template>
</body>
<script src="js/vue.js"></script>
<script>
Vue.component('my-component',{
template: '#myComponent'
})
new Vue({
el: '#app'
})
</script>
</html>
第三種 單文件組件
這種方法常用在vue單頁應(yīng)用中。詳情看官網(wǎng):https://cn.vuejs.org/v2/guide/single-file-components.html
創(chuàng)建.vue后綴的文件,組件Hello.vue,放到components文件夾中
<template>
<div class="hello">
<h1>{{ msg }}</h1>
</div>
</template>
<script>
export default {
name: 'hello',
data () {
return {
msg: '歡迎!'
}
}
}
</script>
app.vue
<!-- 展示模板 -->
<template>
<div id="app">
<img src="./assets/logo.png">
<hello></hello>
</div>
</template>
<script>
// 導(dǎo)入組件
import Hello from './components/Hello'
export default {
name: 'app',
components: {
Hello
}
}
</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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue項(xiàng)目前端部署詳細(xì)步驟(nginx方式)
Nginx(engine x)是一個(gè)高性能的HTTP和反向代理web服務(wù)器,是部署前端項(xiàng)目的首選,這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目前端部署(nginx方式)的相關(guān)資料,需要的朋友可以參考下2023-09-09
vue 和vue-touch 實(shí)現(xiàn)移動(dòng)端左右導(dǎo)航效果(仿京東移動(dòng)站導(dǎo)航)
這篇文章主要介紹了vue 和vue-touch 實(shí)現(xiàn)移動(dòng)端左右導(dǎo)航效果(仿京東移動(dòng)站導(dǎo)航),需要的朋友可以參考下2017-04-04
vue 自定義指令自動(dòng)獲取文本框焦點(diǎn)的方法
今天小編就為大家分享一篇vue 自定義指令自動(dòng)獲取文本框焦點(diǎn)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08
vue v-model實(shí)現(xiàn)自定義樣式多選與單選功能
這篇文章主要介紹了vue v-model實(shí)現(xiàn)自定義樣式多選與單選功能所遇到的問題,本文給大家?guī)砹私鉀Q思路和實(shí)現(xiàn)代碼,需要的朋友可以參考下2018-07-07
利用Electron簡(jiǎn)單擼一個(gè)Markdown編輯器的方法
這篇文章主要介紹了利用Electron簡(jiǎn)單擼一個(gè)Markdown編輯器的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-06-06
Vue使用CDN引用項(xiàng)目組件,減少項(xiàng)目體積的步驟
這篇文章主要介紹了Vue使用CDN引用項(xiàng)目組件,減少項(xiàng)目體積的步驟,幫助大家提高項(xiàng)目加載速度,感興趣的朋友可以了解下2020-10-10

