在
config/index.js文件中添加節(jié)點(diǎn)
const path = require('path');
const config = {
alias: {
'@components': path.resolve(__dirname, '..', 'src/components'),
'@utils': path.resolve(__dirname, '..', 'src/utils')
}
}
但若要在 Sass 中使用別名,請?jiān)?code>config/index.js文件中添加節(jié)點(diǎn)
(Taro 對樣式的處理是 node-sass -> postcss,在 sass 這步就報(bào)錯(cuò)了,不能用 postcss-import 插件解決)
備注:目前資源引用時(shí)仍無法使用別名,如 background: url('@assets/logo.png')
plugins: {
sass: {
importer: function(url) {
const reg = /^@styles\/(.*)/
return {
file: reg.test(url) ? path.resolve(__dirname, '..', 'src/styles', url.match(reg)[1]) : url
}
}
}
}
如上配置,編譯可以通過,但是vscode編輯器爆紅。解決辦法在
tsconfig.json中添加如下內(nèi)容
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@utils": [
"src/utils"
],
"@components": [
"src/components"
],
},
},
}
這樣既可在所在的.tsx文件中使用
import { setGlobalData, getGlobalData } from "@utils";