标准Vue HelloWorld的例子(HelloWorld中的内容简化):
// main.js
new Vue({
el: '#app',
render: h => h(App)
})
// App.vue
<template>
<div id="app">
<img src="./assets/logo.png">
<hello-world></hello-world>
</div>
</template>
<script>
import HelloWorld from './components/HelloWorld'
export default {
name: 'App',
components: {
HelloWorld
},
methods: {
}
}
</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>
<template>
<div class="hello">
<h1>{{ msg }}</h1>
<h2>register component</h2>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
font-weight: normal;
}
</style>
看了大量源码分析的文章,但大多陷入细节,最后绕进去可能还是会一头雾水不知道代码是怎么运行的,所以就画了个完整的流程图来参考,有地图,不迷路,具体的源码可以到vue.esm.js里去搜索来看,这里就不贴了:
其实前面还有一部分编译的工作,就是把template转化成createElement函数供render使用,后面单独再写一篇编译的。