国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

通過npm或yarn自動(dòng)生成vue組件的方法示例

2024-05-06 15:42:31
字體:
供稿:網(wǎng)友

不知道大家每次新建組件的時(shí)候,是不是都要?jiǎng)?chuàng)建一個(gè)目錄,然后新增一個(gè).vue文件,然后寫template、script、style這些東西,如果是公共組件,是不是還要新建一個(gè)index.js用來導(dǎo)出vue組件、雖然有vscode有代碼片段能實(shí)現(xiàn)自動(dòng)補(bǔ)全,但還是很麻煩,今天靈活運(yùn)用scripts工作流,自動(dòng)生成vue文件和目錄。

實(shí)踐步驟

安裝一下chalk,這個(gè)插件能讓我們的控制臺(tái)輸出語句有各種顏色區(qū)分

npm install chalk --save-dev yarn add chalk --save-dev

在根目錄中創(chuàng)建一個(gè) scripts 文件夾

新增一個(gè)generateComponent.js文件,放置生成組件的代碼

新增一個(gè)template.js文件,放置組件模板的代碼

template.js文件,里面的內(nèi)容可以自己自定義,符合當(dāng)前項(xiàng)目的模板即可

// template.jsmodule.exports = { vueTemplate: compoenntName => {  return `<template> <div class="${compoenntName}">  ${compoenntName}組件 </div></template><script>export default { name: '${compoenntName}'}</script><style scoped lang="stylus" rel="stylesheet/stylus">.${compoenntName} {}</style>` }, entryTemplate: `import Main from './main.vue'export default Main`}

generateComponent.js生成vue目錄和文件的代碼

// generateComponent.js`const chalk = require('chalk') // 控制臺(tái)打印彩色const path = require('path')const fs = require('fs')const resolve = (...file) => path.resolve(__dirname, ...file)const log = message => console.log(chalk.green(`${message}`))const successLog = message => console.log(chalk.blue(`${message}`))const errorLog = error => console.log(chalk.red(`${error}`))const { vueTemplate, entryTemplate } = require('./template')const _ = process.argv.splice(2)[0] === '-com'const generateFile = (path, data) => { if (fs.existsSync(path)) {  errorLog(`${path}文件已存在`)  return } return new Promise((resolve, reject) => {  fs.writeFile(path, data, 'utf8', err => {   if (err) {    errorLog(err.message)    reject(err)   } else {    resolve(true)   }  }) })}// 公共組件目錄src/base,全局注冊(cè)組件目錄src/base/global,頁面組件目錄src/components_ ? log('請(qǐng)輸入要生成的組件名稱、如需生成全局組件,請(qǐng)加 global/ 前綴') : log('請(qǐng)輸入要生成的頁面組件名稱、會(huì)生成在 components/目錄下')let componentName = ''process.stdin.on('data', async chunk => { const inputName = String(chunk).trim().toString() // 根據(jù)不同類型組件分別處理 if (_) {  // 組件目錄路徑  const componentDirectory = resolve('../src/base', inputName)  // vue組件路徑  const componentVueName = resolve(componentDirectory, 'main.vue')  // 入口文件路徑  const entryComponentName = resolve(componentDirectory, 'index.js')  const hasComponentDirectory = fs.existsSync(componentDirectory)  if (hasComponentDirectory) {   errorLog(`${inputName}組件目錄已存在,請(qǐng)重新輸入`)   return  } else {   log(`正在生成 component 目錄 ${componentDirectory}`)   await dotExistDirectoryCreate(componentDirectory)  }  try {   if (inputName.includes('/')) {    const inputArr = inputName.split('/')    componentName = inputArr[inputArr.length - 1]   } else {    componentName = inputName   }   log(`正在生成 vue 文件 ${componentVueName}`)   await generateFile(componentVueName, vueTemplate(componentName))   log(`正在生成 entry 文件 ${entryComponentName}`)   await generateFile(entryComponentName, entryTemplate)   successLog('生成成功')  } catch (e) {   errorLog(e.message)  } } else {  const inputArr = inputName.split('/')  const directory = inputArr[0]  let componentName = inputArr[inputArr.length - 1]  // 頁面組件目錄  const componentDirectory = resolve('../src/components', directory)  // vue組件  const componentVueName = resolve(componentDirectory, `${componentName}.vue`)  const hasComponentDirectory = fs.existsSync(componentDirectory)  if (hasComponentDirectory) {   log(`${componentDirectory}組件目錄已存在,直接生成vue文件`)  } else {   log(`正在生成 component 目錄 ${componentDirectory}`)   await dotExistDirectoryCreate(componentDirectory)  }  try {   log(`正在生成 vue 文件 ${componentName}`)   await generateFile(componentVueName, vueTemplate(componentName))   successLog('生成成功')  } catch (e) {   errorLog(e.message)  } } process.stdin.emit('end')})process.stdin.on('end', () => { log('exit') process.exit()})function dotExistDirectoryCreate (directory) { return new Promise((resolve) => {  mkdirs(directory, function () {   resolve(true)  }) })}// 遞歸創(chuàng)建目錄function mkdirs (directory, callback) { var exists = fs.existsSync(directory) if (exists) {  callback() } else {  mkdirs(path.dirname(directory), function () {   fs.mkdirSync(directory)   callback()  }) }}            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 天全县| 沽源县| 大理市| 长沙县| 页游| 大埔区| 武城县| 潍坊市| 江油市| 莎车县| 五峰| 曲水县| 额济纳旗| 上虞市| 延庆县| 鄯善县| 伊通| 嵊泗县| 黄山市| 精河县| 江油市| 治多县| 阜新市| 五大连池市| 浠水县| 江口县| 临沧市| 双流县| 斗六市| 明星| 武邑县| 富平县| 博兴县| 晋中市| 资兴市| 秀山| 秀山| 绥阳县| 涪陵区| 保康县| 江川县|