Vue单文件组件

2019-12-16  本文已影响0人  橙赎
一、概念

Vue自定义了一种.vue文件,可以把html,css,js 写到一个文件中,从而实现了对一个组件的封装

优点

二、使用方法
npm install -g @vue/cli
# OR
yarn global add @vue/cli
vue create my-project       //命令行创建
# OR
vue ui    //可视化界面创建
npm run serve    
<template>
//html
</template>

<script>
//js
export default {
}
</script>

<style>
//css
</style>
<style scoped>    //scoped:表示只作用于当前组件
@import url("assets/css/bbs.css");
</style>

vue官网举例:

//子组件
<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
  </div>
</template>
<script>
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  }
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

//父组件
<template>
  <div id="app">
    <HelloWorld msg="Welcome to Your Vue.js App"/>
  </div>
</template>

<script>
//引用子组件
import HelloWorld from './components/HelloWorld.vue'

export default {
  name: 'app',
  components: {
    HelloWorld
  }
}
</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>

效果:


image.png
上一篇 下一篇

猜你喜欢

热点阅读