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项目后看到的图像,那么图像下面的内容是从哪里渲染出来的呢?

我们注意到,

posted @ 2022-02-11 16:31  千裳~  阅读(543)  评论(2)    收藏  举报