1. 程式人生 > 其它 >vue中index.html、main.js、App.vue、index.js之前的關係以及載入過程--轉載

vue中index.html、main.js、App.vue、index.js之前的關係以及載入過程--轉載

原文連結:https://blog.csdn.net/qq_34182808/article/details/86690193
作者:ONLY&YOU

簡介

專案部署完成後的專案結構以及解釋如下圖所示

專案執行
專案的執行入口index.html

為什麼index.html是專案的入口以及為什麼index.html載入後會繼續載入main.js、App.vue、index.js,以及他們之間的關係是如何聯絡起來的呢,這塊的配置檔案位於build資料夾下,包括webpack.dev.conf.js等,感興趣的可以瞭解下。通過專案的配置檔案,可以載入執行我們的index.html檔案以及自動關聯vue相關的模組。

首先我們來看一下index.html中的內容

<!DOCTYPE html>

<html>

  <head>

    <meta charset="utf-8">

    <meta name="viewport" content="width=device-width,initial-scale=1.0">

    <title>y</title>

  </head>

  <body>

    <div id="app"></div>

    <!-- built files will be auto injected -->

  </body>

</html>

在body體中只有一個div標籤,其id為app,這個id將會連線到src/main.js內容,接著我們看一下main.js中的主要的程式碼

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
 
Vue.config.productionTip = false
 
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

在main.js中,新建了一個vue例項,並使用el:#app連結到index.html中的app,並使用template引入元件和路由相關的內容(具體的涉及到vue的語法規則,如果不理解的先記下來吧,繼續往後看,等了解vue的相關內容後,可能會更清晰)。也就是說通過main.js我們關聯到App.vue元件,接著,我們繼續看一下App.vue元件中的內容。

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <router-view/>
  </div>
</template>
 
<script>
export default {
  name: 'App'
}
</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中的內容,是一個標準的App.vue模板的形式,包含了、、三部分,從template標籤中可以看到,使用img標籤載入了vue的影象,也就是我們使用npm run dev執行vue專案後看到的影象,那麼影象下面的內容是從哪裡渲染出來的呢?

我們注意到,