读源码(五)—— new Vue

本文通过一个标准的Vue HelloWorld例子,详细解析Vue应用的基础结构,包括main.js和App.vue文件,以及组件的导入和使用。同时,强调了模板转化为createElement函数的编译过程,并提供了整体流程图帮助理解。文章旨在提供清晰的学习路径,避免在源码分析中迷失方向。
摘要由CSDN通过智能技术生成

标准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使用,后面单独再写一篇编译的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值