我一直在尝试使用故事书进行角度项目,并使用本指南https://storybook.js.org/basics/guide-angular/ 我使用推荐的webpack配置为scss文件的sass加载器和与项目相关的scss文件工作正常,但如果我在故事index.ts文件中导入一个scss文件,则不加载该文件。
故事/ index.ts
import { storiesOf } from '@storybook/angular';
import { action } from '@storybook/addon-actions';
import { VideoPosterComponent } from '../src/app/modules/ui-common/video-poster/video-poster.component';
//This scss it is not loaded
import '../src/styles.scss';
storiesOf('Video Poster component', module)
.add('Video Poster with author data', () => ({
component: VideoPosterComponent,
props: {
title: "Cinemagraph With Custom title",
subtitle: "This is a custom subtitle!"
}
}))
.add('Video Poster without author data', () => ({
component: VideoPosterComponent,
props: {}
}));
.storybook / webpack.config.js (此处推荐 - > https://storybook.js.org/basics/guide-angular/#configure-style-rules)
const genDefaultConfig = require('@storybook/angular/dist/server/config/defaults/webpack.config.js');
module.exports = (baseConfig, env) => {
const config = genDefaultConfig(baseConfig, env);
// Overwrite .css rule
const cssRule = config.module.rules.find(rule => rule.test && rule.test.toString() === '/\\.css$/');
if (cssRule) {
cssRule.exclude = /\.component\.css$/;
}
// Add .scss rule
config.module.rules.unshift({
test: /\.scss$/,
loaders: ['raw-loader', 'sass-loader'],
});
return config;
};
并且,我的组件的scss文件加载没有问题
的src /应用程序/模块/ UI-公共/视频海报/视频poster.component.ts
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-video-poster',
templateUrl: './video-poster.component.html',
styleUrls: ['./video-poster.component.scss'] // this were loaded without problems
})
export class VideoPosterComponent implements OnInit {
private hostUrl = 'https://s3-eu-west-1.amazonaws.com/video.gallereplay.com/portfolio/clients';
private baseUrl = `${this.hostUrl}/jaegermeister/Cinemagraph_plain/1920x1080`;
@Input()
public videoUrls = {
poster: `${this.baseUrl}/cinemagraph.jpg`,
mp4: `${this.baseUrl}/cinemagraph.mp4`,
webm: `${this.baseUrl}/cinemagraph.webm`,
}
@Input() public title = 'Custom Cinemagraph Productions';
@Input() public subtitle = 'Exclusive Content for Businesses';
constructor() { }
ngOnInit() {
}
}
存储库: https://github.com/gpincheiraa/storybook-components-sample
运行npm install && npm run storybook
以检查故事书的运行情况。
我做错了什么?
答案 0 :(得分:0)
所有样式导入必须位于Component元数据中(特别是在styles或styleUrls字段中)。你试图将样式文件导入为js文件,这是错误的。
答案 1 :(得分:0)
您正在尝试从打字稿中导入scss文件。您必须从scss中添加它。
请删除:
//This scss it is not loaded
import '../src/styles.scss';
在您的scss文件中添加:
@import "styles.scss";
在您angular.json中添加:
"styles": [
"src/styles.scss",
],
"stylePreprocessorOptions": {
"includePaths": [
"src/" // maybe not required in your case
]
},
在您.storybook / webpack.config.js中,请尝试:
const path = require("path");
module.exports = {
module: {
rules: [
{
test: /\.scss$/,
loaders: ["sass-loader"],
include: path.resolve(__dirname, "../src/")
}
]
}
};
否则,如Xdecus在此处https://github.com/storybooks/storybook/issues/3814#issuecomment-401756776
所述,可以添加别名。const path = require('path');
module.exports = {
resolve: {
alias: {
styles: path.resolve(__dirname, '../src/')
}
}
};
答案 2 :(得分:0)
我假设像我一样,您正在寻找一种将SASS文件中的全局样式加载到Angular Storybook中的方法。自您提出要求以来,我已经知道已经有一段时间了,但是我在寻找一种解决方案时遇到了这个解决方案:https://github.com/storybookjs/storybook/issues/6364#issuecomment-485651328。
基本上,您可以在Storybook配置文件中加载全局样式。但是,您需要在导入路径中使用内联Webpack加载程序,以便Storybook可以正确加载它们。
import '!style-loader!css-loader!sass-loader!../src/styles.scss';
那对我有用。最后,我不必费心使用自定义的webpack配置。希望能解决您的问题!