Vue从入门到实战:Vue CLI项目结构
来自CloudWiki
目录结构
$ ls
babel.config.js node_modules/ package.json package-lock.json public/ README.md src/
- public:引入的第三方库的JS文件可以放在这里
里面包含项目的主页面
$ ls public
favicon.ico index.html
- src:项目代码的主目录
--assets:项目静态资源,如图片等
--components:编写的组件放在这个目录下
--App.vue: 项目的根组件
--main.js 程序入口js文件,加载各种公共组件和所需要用到的插件
$ ls src
App.vue assets/ components/ main.js
- package.json: npm的配置文件,其中设定了脚本和项目依赖的库。
App.vue
项目的根组件
这是一个典型的单文件组件,在文件中包含了组件代码、模板代码和CSS样式规则。
<template> <div id="app"> <!-- <img alt="Vue logo" src="./assets/logo.png" /> <HelloWorld msg="Welcome to Your Vue.js App" /> --> <Hello /> </div> </template> <script> //import HelloWorld from "./components/HelloWorld.vue"; import Hello from "@/components/Hello"; export default { name: "app", components: { Hello } }; </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>
main.js
程序入口js文件,
加载各种公共组件和所需要用到的插件
import Vue from "vue"; import App from "./App.vue"; Vue.config.productionTip = false; new Vue({ render: h => h(App) }).$mount("#app");
解释:创建Vue实例,使用render函数渲染App组件,并挂载到名为app的HTML元素上
index.html
主页面:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <link rel="icon" href="<%= BASE_URL %>favicon.ico"> <title>helloworld</title> </head> <body> <noscript> <strong>We're sorry but helloworld doesn't work properly without JavaScript enabled. Please enable it to continue.</strong> </noscript> <div id="app"></div> <!-- built files will be auto injected --> </body> </html>
注意:在编译后的页面上只会存在一个id属性名为app的元素,就是index.html中的div元素。