Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vue props and data not working #9956

Closed
armadillo-dev opened this issue Feb 26, 2020 · 10 comments
Closed

Vue props and data not working #9956

armadillo-dev opened this issue Feb 26, 2020 · 10 comments

Comments

@armadillo-dev
Copy link

Describe the bug

I wanna build storybook with nuxt + typescript.
When I pass props or define data in component, I show below message.

[Vue warn]: Property or method "dataTest" is not defined on the instance but referenced during render. 
Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. 
See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

To Reproduce
Steps to reproduce the behavior:
1. setting config .storybook/main.js

const path = require('path');

module.exports = {
    webpackFinal: (config) => {
        config.resolve = {
            extensions: ['.js', '.ts', '.vue', '.json'],
            alias: {
                vue$: path.resolve(__dirname, '../node_modules/vue/dist/vue.esm.js'),
                '@': path.resolve(__dirname, '../'),
            },
        };

        return config
    },
    stories: ['../stories/**/*.stories.[tj]s'],
    addons: [
        '@storybook/preset-scss',
        {
            name: '@storybook/preset-typescript',
            options: {
                tsLoaderOptions: {
                    configFile: path.resolve(__dirname, '../tsconfig.json'),
                },
                include: [path.resolve(__dirname, '../')],
            },
        }
    ]
};

2. define component. TestComponent.vue

<template>
  <div>
    <p>$attrs: {{ $attrs }}</p>
    <p>$props: {{ $props }}</p>
    <p>data: {{ dataTest }}</p>
  </div>
</template>

<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator'

@Component
export default class TestComponent extends Vue {
  @Prop({ type: String })
  propTest

  dataTest: string = 'test1234'
}
</script>

3. write stories TestComponent.stories.ts

import TestComponent from './TestComponent.vue'

export default {
  title: 'TestComponent',
}

export const Default = () => ({
  components: { TestComponent },
  template: `
    <test-component
      propTest="propTest" 
    />
  `,
})

4. run storybook with npm run storybook

Expected behavior
render below

$attrs: 
$props: { "propTest": "propTest" }
$data: test1234

Actual behavior

$attrs: { "propTest": "propTest" }
$props: 
$data:

with warn

System:

Environment Info:

  System:
    OS: macOS Mojave 10.14.6
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
  Binaries:
    Node: 10.15.3 - /usr/local/bin/node
    Yarn: 1.15.2 - /usr/local/bin/yarn
    npm: 6.13.7 - /usr/local/bin/npm
  Browsers:
    Chrome: 79.0.3945.130
    Firefox: 69.0.3
    Safari: 13.0.5
  npmPackages:
    @storybook/addon-actions: ^5.3.13 => 5.3.13 
    @storybook/addon-links: ^5.3.13 => 5.3.13 
    @storybook/addons: ^5.3.13 => 5.3.13 
    @storybook/preset-scss: ^1.0.2 => 1.0.2 
    @storybook/preset-typescript: ^1.2.0 => 1.2.0 
    @storybook/vue: ^5.3.13 => 5.3.13 

@Aaron-Pool
Copy link
Contributor

@armadillo-dev I think you need to specify in your ts-loader options to use .vue files as well as .ts files.

@armadillo-dev
Copy link
Author

@Aaron-Pool thank you for you comment.
I removed @storybook/preset-typescript package.
And then modified config in .storybook/main.js
It working!

module.exports = {
  webpackFinal: (config) => {
    config.module.rules.push({
      test: /\.(ts|tsx)$/,
      use: [{
        loader: require.resolve('ts-loader'),
        options: { appendTsSuffixTo: [/\.vue$/] }
      }],
    });
    config.resolve.extensions.push('.ts', '.tsx');
    ...
    return config
  },
 ...
}

@graup
Copy link
Contributor

graup commented Feb 28, 2020

@shilman Let's add this to the vue docs. Do you think adding a "Using with Typescript" section to the vue guide would suffice? Or should we make the preset more intelligent somehow so this extra step could be eliminated?

@shilman
Copy link
Member

shilman commented Feb 28, 2020

@graup Good question. I think the best thing would be to make the typescript preset smarter since Vue is one of the major frameworks (React, Vue, Angular) we want to support, and therefore I don't mind hard-coding it in the preset.

function webpack(webpackConfig = {}, options = {}) {
 ...
  if(options.framework === 'vue) {
    rules.push(...)
  }
}

That said, the preset needs some love and I haven't had the bandwidth go get to it for the past month... 😭

@graup
Copy link
Contributor

graup commented Feb 28, 2020

Good, I'll make a PR for this as well then.

@shilman
Copy link
Member

shilman commented Feb 28, 2020

@graup 감사합니다!

@BennaceurHichem
Copy link

@graup @shilman I tried to add the config mentioned below to my main.ts, but in my case when I launch Storybook again it crashes, and the reason is probably that that I don't have ts-loader in my case, should I install it, and is there any changes that I should do after installation to make things working?
I'm using @nuxt/storybook module, and @storybook/preset-typescript is not installed

@graup
Copy link
Contributor

graup commented Jun 25, 2021

Hi @BennaceurHichem, you should not need any extra config anymore. Ts-loader is required but should be installed automatically as a dependency of storybook/vue. Could you open a new issue with the error you are encountering?

@BennaceurHichem
Copy link

BennaceurHichem commented Jun 25, 2021

@graup , I'm using nuxt-storybook module, I will open a new issue with more details

@LobergDesign
Copy link

@BennaceurHichem I´m having the same issue regarding nuxt-storybook. Can you send a link to the issue you have created? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants