简体   繁体   English

我如何在vuejs的故事书故事中导入scss文件和图像?

[英]How can i import scss files and images in stories of storybook in vuejs?

I have a simple component as you can see here, which imports an image via src="@/assets/images/logo.png" using @ for addressing :我有一个简单的组件,您可以在此处看到,它通过src="@/assets/images/logo.png"使用@ for addressing来导入图像:

<template>
  <div class="loading_container">
    <img
      class="loading_logo"
      src="@/assets/images/logo.png"
      alt="company logo"
    />
    <div class="loading_box">
      <div class="loading_dot"></div>
      <div class="loading_dot"></div>
      <div class="loading_dot"></div>
      <div class="loading_dot"></div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent } from "@vue/composition-api";
export default defineComponent({
  name: "Loading"
});
</script>

And this is my story, which i imported here but it seems to not working:这是我的故事,我在这里导入但它似乎不起作用:

import Loading from "../src/views/components/loading/Loading";
import  "../src/assets/styles/components/_Loading.scss";
export default {
  title: "Loading"
};

export const normalLoading = () => ({
  components: { Loading },
  template: "<Loading></Loading>",
});

When I use npm run storybook, it will show two errors, one for each one of the above issues.当我使用 npm 运行 storybook 时,它会显示两个错误,每个错误对应上述问题之一。 how should I fix these issues?我应该如何解决这些问题?

update更新

The error for image is:图片的错误是:

    ERROR in ./src/views/components/loading/Loading.vue?vue&type=template&id=1f4267ea& (./node_modules/vue-loader/lib/loaders/templateLoader.js??vue-loader-options!./node_modules/vue-loader/lib??vue-loader-options!./src/views/components/loading/Loading.vue?vue&type=template&id=1f4267ea&)
Module not found: Error: Can't resolve '@/assets/images/logo.png' in 'C:\Projects\my_github\vuejs-persian-chat-scaffold\src\views\components\loading'
 @ ./src/views/components/loading/Loading.vue?vue&type=template&id=1f4267ea& (./node_modules/vue-loader/lib/loaders/templateLoader.js??vue-loader-options!./node_modules/vue-loader/lib??vue-loader-options!./src/views/components/loading/Loading.vue?vue&type=template&id=1f4267ea&) 15:22-57
 @ ./src/views/components/loading/Loading.vue?vue&type=template&id=1f4267ea&
 @ ./src/views/components/loading/Loading.vue
 @ ./stories/loading.stories.js
 @ ./stories sync ^\.\/(?:(?:(?!\.)(?:(?:(?!(?:|[\\/])\.).)*?)[\\/])?(?!\.)(?=.)[^\\/]*?\.stories\.js[\\/]?)$
 @ ./.storybook/generated-entry.js
 @ multi ./node_modules/@storybook/core/dist/server/common/polyfills.js ./node_modules/@storybook/core/dist/server/preview/globals.js ./.storybook/generated-entry.js (webpack)-hot-middleware/client.js?reload=true&quiet=true

and here is my ./storybook/webpack.config.js file which is working on addressing like import "../src/assets/styles/components/_Loading.scss";这是我的./storybook/webpack.config.js文件,它正在处理import "../src/assets/styles/components/_Loading.scss"; but i want to use @/ for addressing:但我想使用@/来寻址:

const path = require("path");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = function({ config }) {
  config.module.rules.push({
    test: /\.scss$/,
    loaders: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"],
    include: path.resolve(__dirname, "../")
  });
  config.plugins.push(new MiniCssExtractPlugin({ filename: "[name].css" }));

  return config;
};

Since it seems this problem is widespread, I asked on storybook's GitHub and it seems they need to add compatibility mode for this.由于这个问题似乎很普遍,我在故事书的 GitHub上询问过,他们似乎需要为此添加兼容模式。

I've asked the question on storybook's github, and I got an answer to use vue-cli to fix the problem.我在storybook的github上问过这个问题,得到了使用vue-cli解决问题的答案。

So, First I stashed all changes which have been made by following the instructions of https://storybook.js.org/docs/guides/guide-vue/ .因此,首先我隐藏了按照https://storybook.js.org/docs/guides/guide-vue/的说明所做的所有更改。

Then I've re-installed storybook via using vue add storybook ( https://github.com/storybookjs/vue-cli-plugin-storybook ) which creates a folder that contains webpack configs from vue-cli, which means you don't need to have any special webpack section in storybook config folder.然后我通过使用vue add storybookhttps://github.com/storybookjs/vue-cli-plugin-storybook )重新安装了故事书,它创建了一个文件夹,其中包含来自 vue-cli 的 webpack 配置,这意味着你不故事书配置文件夹中不需要任何特殊的 webpack 部分。 the @ problem has been solved and every thing is fine @问题已经解决,一切都很好

<img
  class="loading_logo"
  src="@/assets/images/logo.png"
  alt="company logo"
/>

But still there was one error for addressing assets in .scss url() like content:但是在.scss url()中寻址资产时仍然存在一个错误,例如内容:

url("assets/images/tick.svg"); 

I fixed it by using relative addressing using ~@ together like below:我通过使用~@一起使用相对寻址来修复它,如下所示:

content: url("~@/assets/images/tick.svg");

the working example is here:工作示例在这里:

https://github.com/SeyyedKhandon/vuejs-persian-chat-scaffoldhttps://github.com/SeyyedKhandon/vuejs-persian-chat-scaffold

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM