日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
怎么通過npm或yarn自動(dòng)生成vue組件

這篇文章主要介紹“怎么通過npm或yarn自動(dòng)生成vue組件”,在日常操作中,相信很多人在怎么通過npm或yarn自動(dòng)生成vue組件問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”怎么通過npm或yarn自動(dòng)生成vue組件”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

十多年的老河口網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。全網(wǎng)營(yíng)銷推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整老河口建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“老河口網(wǎng)站設(shè)計(jì)”,“老河口網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

實(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.js
module.exports = {
 vueTemplate: compoenntName => {
  return `




.${compoenntName} {

}


`
 },
 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()
  })
 }
}

配置package.json,scripts新增兩行命令,其中-com是為了區(qū)別是創(chuàng)建頁面組件還是公共組件

"scripts": {
  "new:view":"node scripts/generateComponent",
  "new:com": "node scripts/generateComponent -com"
 },

執(zhí)行

  npm run new:view // 生成頁組件
  npm run new:com // 生成基礎(chǔ)組件
  或者
  yarn run new:view // 生成頁組件
  yarn run new:com // 生成基礎(chǔ)組件

怎么通過npm或yarn自動(dòng)生成vue組件

到此,關(guān)于“怎么通過npm或yarn自動(dòng)生成vue組件”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!


當(dāng)前名稱:怎么通過npm或yarn自動(dòng)生成vue組件
本文URL:http://www.dlmjj.cn/article/jpdoih.html