Answer the question
In order to leave comments, you need to log in
Vue + TypeScript css modules?
import stylesCommon from '@/assets/common/scss/common.scss'; // импортирует что надо,
// но ситили применить нельзя т.к. модули неработают
import stylesFonts from '@fortawesome/fontawesome-free'; // пакет установлен, но импорт пустой!?
Vue.mixin(
CSSModules({
injectAttr: 'class',
styles: {...stylesFonts, ...stylesCommon},
}),
);
@Template
@Component({
mixins: [CSSModules({
injectAttr: 'class',
styles: {...stylesFonts, ...stylesCommon},
}),
],
router,
})
class Main extends Vue {
public mounted(): void {
this.logWarning();
console.log(stylesCommon);
console.log(stylesFonts);
console.log(CSSModules({
injectAttr: 'class',
styles: {...stylesFonts, ...stylesCommon},
}),);
}
{h1: "h1", h2: "h2", h3: "h3", h4: "h4", h5: "h5", …}
main.ts?cd49:60
// 2
{} __proto__: Object
// 3
{beforeCreate: ƒ}
beforeCreate: ƒ beforeCreate() __proto__: Object
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
importLoaders: 1,
modules: true,
localIdentName: '[local]',
sourceMap: true,
},
},
],
},
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader, //'style-loader',
{
loader: 'css-loader',
options: {
importLoaders: 1,
modules: true,
localIdentName: '[local]',
sourceMap: true,
},
},
'sass-loader',
],
},
{
// "compileOnSave": true,
"compilerOptions": {
"module": "es2020",
"outDir": "./dist/",
"sourceMap": true,
"strict": true,
"strictFunctionTypes": false,
"noEmitHelpers": true,
"experimentalDecorators": true,
"importHelpers": true,
"allowSyntheticDefaultImports": true,
"emitDecoratorMetadata": true,
"strictNullChecks": true,
"noImplicitReturns": true,
"noImplicitAny": false,
"moduleResolution": "node",
"target": "es6",
"lib": [
"dom",
"es6",
"es2020"
],
"typeRoots": [
"./node_modules/@types"
],
"baseUrl": ".",
"paths": {
"~/*": [
"./src/*"
],
"@/*": [
"./src/*"
]
}
},
"exclude": [
"backup",
"dist",
"build",
"scripts",
"acceptance-tests",
"webpack",
"jest",
"src/setupTests.ts",
"node_modules",
"obj",
"**/*.spec.ts"
],
"include": [
"src",
"src/**/*.ts",
"@types/**/*.d.ts"
]
}
TS2339: Property '$route' does not exist on type 'Main'.
Vue.use(VueRouter);
export default new VueRouter({
import Template from '@/index.html?style=./index.scss'
import * as Template from '@/index.html?style=./index.scss'
Answer the question
In order to leave comments, you need to log in
And so, after 5-6 hours I found a solution, and even more than one
1 - I used this plugin to inject it with a mixon in the main main.ts file.
2 - console.log console.log(CSSModules({
as it should work
3 - it is important to specify the attr parameter injectAttr: 'class',
to avoid using class="$style.container"
the $styles parameter
4 - $style will not really work, in order for it to work, you need to prescribe some tricks with the .d.ts file
// 1. Make sure to import 'vue' before declaring augmented types
// 2. Specify a file with the types you want to augment
// Vue has the constructor type in types/vue.d.ts
declare module 'vue/types/vue' {
// 3. Declare augmentation for Vue
interface Vue {
// $style: { [key: string]: string }
$style: any
}
}
declare module '*.css' {
import Vue, {ComponentOptions, FunctionalComponentOptions} from 'vue'
interface Template2 {
<V extends Vue, U extends ComponentOptions<V> | FunctionalComponentOptions>(options: U): U
<V extends typeof Vue>(component: V): V
}
const template2: Template2;
export = template2
}
declare module '*.scss' {
import Vue, {ComponentOptions, FunctionalComponentOptions} from 'vue'
interface Template {
<V extends Vue, U extends ComponentOptions<V> | FunctionalComponentOptions>(options: U): U
<V extends typeof Vue>(component: V): V
}
const template: Template;
export = template
}
import Template from './app.pug?style=./app.scss'
import * as Template from '@/index.html?style=./index.scss'
import * as test from '@/index.scss';
// миксин для инжекта local css которые подтянуты template-loader '@/index.html?style=./index.scss'
Vue.mixin(
CSSModules({
// be careful, 'class' also use by 3-d parties libs classes
injectAttr: 'class',
// styles are merged with this.$style by default
styles: {...stylesFonts, ...stylesCommon, ...test},
}),
);
"vue": "^2.6.11",
"vue-class-component": "^7.2.3",
"vue-css-modules": "https://github.com/BonBonSlick/vue-css-modules.git", // для инжекта стилей в $style
"vue-template-loader": "^1.1.0", // для инжекта стилей в сборку, без етого css-modules не работает
"vue-property-decorator": "^8.4.2", // для рендеринга
<h1 :class="[$style.hello, $style['text-danger']
>
can be used without mixin, but no global styles, $style still not accessible
style "text-danger" is not available!
</h1>
<hr>
<h2 class="hello">
injected with template-loader and mixin, global and local styles available. modules
</h2>
h1 class="XD_i__hello text-danger">
can be used without mixin, but no global styles, $style still not accessible
style "text-danger" is not available!
</h1>
<h2 class="XD_i__hello _2qzp__text-danger">
injected with template-loader and mixin, global and local styles available. modules
</h2>
<main :id="$style.app"
declare module '*.vue' {
interface Vue {
$style: any
}
export default Vue;
}
extends Vue
as templates, $styles is injected
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question