vue3項(xiàng)目目錄結(jié)構(gòu)示例詳解
一、vue3項(xiàng)目的目錄結(jié)構(gòu)詳解
- node_modules:通過
npm install
下載安裝的項(xiàng)目依賴包 - public:存放靜態(tài)資源公共資源,不會(huì)被壓縮合并
- —3DModel:存放.glb3D模型
- —favicon.ico:網(wǎng)站圖標(biāo)
- —index.html:首頁入口.html文件
- src:項(xiàng)目開發(fā)主要文件夾
- —api
- —assets:靜態(tài)文件,存放圖片
- —components:存放組件
- —store:與vuex相關(guān)
- —styles:存放樣式
- —utils
- —views:界面組件
- —App.vue:根組件
- —main.js:入口文件
- —router.js:存放路由,實(shí)現(xiàn)界面跳轉(zhuǎn)
- .gitignore:用來配置那些文件不歸git管理
- package.json:項(xiàng)目配置和包管理文件(項(xiàng)目依賴和技術(shù))
- README.md:說明文檔,主要看項(xiàng)目運(yùn)行的命令
- vue.config.js:項(xiàng)目配置信息:跨域proxy代理
二、部分主要文件詳解
1、index.html
<%= BASE_URL %>
<%= htmlWebpackPlugin.options.title %>
將vue實(shí)例掛載到id為#app的dom上
<!DOCTYPE html> <html lang=""> <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" rel="external nofollow" > <link rel="stylesheet" rel="external nofollow" > <title> <%= htmlWebpackPlugin.options.title %> </title> </head> <body> <noscript> <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong> </noscript> <!-- 將vue實(shí)例掛載到id為#app的dom上 --> <div id="app"></div> <!-- built files will be auto injected --> <style> /* 公共樣式可以寫在這里 */ </style> </body> </html>
2、main.js(main.ts)
導(dǎo)入組件、路由等
vue3使用createApp這個(gè)api返回一個(gè)應(yīng)用實(shí)例,并且可以鏈?zhǔn)秸{(diào)用;這也是與vue2.0不同之處,vue2.0是通過new Vue() 來創(chuàng)建一個(gè)vue實(shí)例的
createApp(App).use(store).use(router).mount(‘#app’);
import { createApp } from 'vue' import App from './App.vue' import router from './router' import store from './store/index' import * as Api from './api/index.js' import ElementPlus from 'element-plus' import 'element-plus/dist/index.css' import 'dayjs/locale/zh-cn' //中文 import locale from 'element-plus/lib/locale/lang/zh-cn' //中文 import * as ElIcons from '@element-plus/icons-vue' import permission from '@/constants/permission'; import UUID from 'vue-uuid'; import "vis/dist/vis.css"; var cesium = require('cesium/Cesium.js'); var widgets = require('cesium//Widgets/widgets.css'); // 創(chuàng)建實(shí)例 const app = createApp(App); // 全局變量、屬性 app.config.globalProperties.$api = Api app.config.globalProperties.Cesium = cesium app.config.globalProperties.Widgets = widgets // 添加widow對(duì)象 window.Cesium = cesium; // 統(tǒng)一注冊(cè)Icon圖標(biāo) for (const iconName in ElIcons) { app.component(iconName, ElIcons[iconName]) } // 自定義指令實(shí)現(xiàn)權(quán)限控制 app.directive("permission", permission) // app.use() 注冊(cè)全局組件 app.use(ElementPlus, { locale }); app.use(store); app.use(router); app.use(UUID); // 應(yīng)用實(shí)例掛載到#app中,一定要放在最后面 app.mount('#app');
3. package.json
a. scripts
可以使用npm run serve或yarn serve查看項(xiàng)目效果,就是因?yàn)橛衟ackage.json中的scripts起到了作用。
能使用vue-cli-service是因?yàn)関ue-cli自動(dòng)安裝了cli-service這個(gè)工具
- serve : 在開發(fā)時(shí)用于查看效果的命令,視頻中演示看一下效果
- build : 打包打碼,一般用于生產(chǎn)環(huán)境中使用
- lint : 檢查代碼中的編寫規(guī)范
- b. dependencies 生產(chǎn)環(huán)境 devDependencies 開發(fā)環(huán)境
這兩個(gè)都是用來記錄安裝包信息的,
生產(chǎn)環(huán)境:就是代碼已經(jīng)上線,放到了正式的服務(wù)器上,公司開始運(yùn)營(yíng)去賺錢的代碼。
開發(fā)環(huán)境: 作為一個(gè)程序員,每天作的事情都是在開發(fā)環(huán)境中,編寫代碼、測(cè)試代碼、修改 Bug。也就說這些代碼沒有上線。
dependencies下的包是生產(chǎn)環(huán)境中必須用到的,當(dāng)然開發(fā)環(huán)境也需要。devDependencies是只有開發(fā)環(huán)境中使用的,上線后這些包就沒用了,打包后也不會(huì)打包進(jìn)去的代碼。
"name": "simulator", "version": "0.1.0", "private": true, "scripts": { "serve": "vue-cli-service serve", "build": "vue-cli-service build", "lint": "vue-cli-service lint" }, "dependencies": { "@element-plus/icons-vue": "^1.1.4", "axios": "^0.26.1", "cesium": "*", "core-js": "^3.8.3", "element-plus": "^2.1.2", "file-loader": "^6.2.0", "js-md5": "^0.7.3", "node-sass": "^7.0.1", "nprogress": "^0.2.0", "pdfjs-dist": "^2.4.456", "sass-loader": "^12.6.0", "url-loader": "^4.1.1", "vis": "^4.21.0-EOL", "vue": "^3.2.13", "vue-pdf": "^4.3.0", "vue-router": "^4.0.14", "vue-uuid": "^3.0.0", "vue3-pdf": "^4.2.6", "vuedraggable": "^4.1.0", "vuex": "^4.0.2" }, "devDependencies": { "@babel/core": "^7.12.16", "@babel/eslint-parser": "^7.12.16", "@babel/plugin-proposal-nullish-coalescing-operator": "^7.16.7", "@babel/plugin-proposal-optional-chaining": "^7.16.7", "@babel/plugin-proposal-private-methods": "^7.16.11", "@types/pdfjs-dist": "^2.10.378", "@vue/cli-plugin-babel": "~5.0.0", "@vue/cli-plugin-eslint": "~5.0.0", "@vue/cli-service": "~5.0.0", "copy-webpack-plugin": "^5.1.2", "eslint": "^7.32.0", "eslint-plugin-vue": "^8.0.3" }, "eslintConfig": { "root": true, "env": { "node": true }, "extends": [ "plugin:vue/vue3-essential", "eslint:recommended" ], "parserOptions": { "parser": "@babel/eslint-parser" }, "rules": { "vue/multi-word-component-names": "off", "no-unused-vars": "off", "no-undef": "off" } }, "browserslist": [ "> 1%", "last 2 versions", "not dead", "not ie 11" ] }
三、其他說明
1. node版本錯(cuò)誤
注意,不同的node版本,安裝依賴包可能會(huì)報(bào)錯(cuò)
在vue3項(xiàng)目中,我使用的node版本是 v14.15.1
推薦nvm對(duì)不同的node版本進(jìn)行管理
nvm :node版本管理器
2. 如何調(diào)用全局屬性
如果在其他.vue文件中你使用的是<script setup>
,你會(huì)發(fā)現(xiàn)直接使用this.$api會(huì)報(bào)錯(cuò),
這里提供一個(gè)新的api:getCurrentInstance 獲取vue當(dāng)前實(shí)例,掛載在全局上的東西都需要通過實(shí)例去拿
未親測(cè)過,感興趣的可以親測(cè)過告訴我可行性~
import { getCurrentInstance } from "vue"; // 拿到實(shí)例 const _this = getCurrentInstance (); // 在實(shí)例中有個(gè)ctx上下文 _this.ctx.$api
我直接用的是<script>
即vue2.0寫法編寫js代碼,則可以直接使用this.$api
3. vue文件中應(yīng)用vue3.0的api
vue3.0中用setup去包含了所有的鉤子等,所有使用的鉤子都需要提前導(dǎo)入
總結(jié)
到此這篇關(guān)于vue3項(xiàng)目目錄結(jié)構(gòu)的文章就介紹到這了,更多相關(guān)vue3項(xiàng)目目錄結(jié)構(gòu)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3中的element-plus表格實(shí)現(xiàn)代碼
這篇文章主要介紹了Vue3中的element-plus表格實(shí)現(xiàn)代碼,用組件屬性實(shí)現(xiàn)跳轉(zhuǎn)路由,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05vuecli3.0腳手架搭建及不同的打包環(huán)境配置vue.config.js的詳細(xì)過程
這篇文章主要介紹了vuecli3.0腳手架搭建及不同的打包環(huán)境配置vue.config.js的詳細(xì)過程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01Vue項(xiàng)目中使用fontawesome圖標(biāo)庫的方法
fontawesome的圖標(biāo)有免費(fèi)版和專業(yè)版,本文主要使用free版本,一般free版本的圖標(biāo)夠用,free圖標(biāo)又劃分為三個(gè)圖標(biāo)庫,主要有實(shí)心圖標(biāo)solid、常規(guī)圖標(biāo)regular及品牌圖標(biāo)brand,根據(jù)需求去下載對(duì)應(yīng)的圖標(biāo)庫,無須全部下載,對(duì)vue?fontawesome圖標(biāo)庫相關(guān)知識(shí)感興趣的朋友一起看看吧2023-12-12ElementUi中select框在頁面滾動(dòng)時(shí)el-option超出元素區(qū)域的問題解決
本文主要介紹了ElementUi中select框在頁面滾動(dòng)時(shí)el-option超出元素區(qū)域的問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08vue-cli解決IE瀏覽器sockjs-client錯(cuò)誤方法
這篇文章主要為大家介紹了vue-cli解決IE瀏覽器sockjs-client錯(cuò)誤方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08vue 自定義提示框(Toast)組件的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue 自定義提示框(Toast)組件的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08利用vue實(shí)現(xiàn)密碼輸入框/驗(yàn)證碼輸入框
這篇文章主要為大家詳細(xì)介紹了如何利用vue實(shí)現(xiàn)密碼輸入框或驗(yàn)證碼輸入框的效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2023-08-08