Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript “如何动态变异”;args";从组件';什么行动?_Javascript_Typescript_Vue.js_Storybook - Fatal编程技术网

Javascript “如何动态变异”;args";从组件';什么行动?

Javascript “如何动态变异”;args";从组件';什么行动?,javascript,typescript,vue.js,storybook,Javascript,Typescript,Vue.js,Storybook,让我们看看我们有一个简单的组件ToggleButton: const ButtonComponent=Vue.component('ButtonComponent'{ 道具:{ 值:布尔值 }, 方法:{ handleClick(){ 这是。$emit('toggle'); } }, 模板:` 切换 ` }); 以及该组件的故事: import-ToggleButton from./ToggleButton.vue'; 导出默认值{ 标题:“切换按钮”, 组件:切换按钮, argtype:{

让我们看看我们有一个简单的组件
ToggleButton

const ButtonComponent=Vue.component('ButtonComponent'{
道具:{
值:布尔值
},
方法:{
handleClick(){
这是。$emit('toggle');
}
},
模板:`
切换
`
});
以及该组件的故事:

import-ToggleButton from./ToggleButton.vue';
导出默认值{
标题:“切换按钮”,
组件:切换按钮,
argtype:{
onToggle:{

动作:'toggle'/我也遇到了同样的问题,并且持续寻找了好几天,直到我偶然发现了这篇github帖子:

目前,在我的React中(我确信vue方法将类似),我执行以下操作:

import React from 'react';
import CheckboxGroupElement from '../CheckboxGroup';
import { STORYBOOK_CATEGORIES } from 'elements/storybook.categories';
import { useArgs } from '@storybook/client-api';

export default {
  component: CheckboxGroupElement,
  title: 'Components/CheckboxGroup',
  argTypes: {
    onChange: {
      control: 'func',
      table: {
        category: STORYBOOK_CATEGORIES.EVENTS,
      },
    },
  },
  parameters: { actions: { argTypesRegex: '^on.*' } },
};

const Template = (args) => {
  const [_, updateArgs] = useArgs();

  const handle = (e, f) => {
// inside this function I am updating arguments, but you can call it anywhere according to your demand, the key solution here is using `useArgs()`
// As you see I am updating list of options with new state here
    console.log(e, f);
    updateArgs({ ...args, options: e });
  };
  return <CheckboxGroupElement {...args} onChange={handle} />;
};

export const CheckboxGroup = Template.bind({});
CheckboxGroup.storyName = 'CheckboxGroup';
CheckboxGroup.args = {
//Here you define default args for your story (initial ones)
  controller: { label: 'Group controller' },
  options: [
    { label: 'option 1', checked: true },
    { label: 'option 2', checked: false },
    { label: 'option 3', checked: false },
  ],
  mode: 'nested',
};
从“React”导入React;
从“../CheckboxGroup”导入CheckboxGroupElement;
从'elements/STORYBOOK.CATEGORIES'导入{STORYBOOK_CATEGORIES};
从“@storybook/client-api”导入{useArgs};
导出默认值{
组件:CheckboxGroupElement,
标题:“组件/复选框组”,
argtype:{
一旦改变:{
控件:“func”,
表:{
类别:故事书_CATEGORIES.EVENTS,
},
},
},
参数:{actions:{argTypesRegex:'^on.*'}},
};
常量模板=(参数)=>{
const[_,updateArgs]=useArgs();
常量句柄=(e,f)=>{
//在这个函数中,我正在更新参数,但是您可以根据需要在任何地方调用它,这里的关键解决方案是使用`useArgs()`
//如您所见,我正在使用新状态更新选项列表
控制台日志(e,f);
updateArgs({…args,选项:e});
};
返回;
};
export const CheckboxGroup=Template.bind({});
CheckboxGroup.storyName='CheckboxGroup';
CheckboxGroup.args={
//这里为您的故事定义默认参数(初始参数)
控制器:{标签:'组控制器'},
选项:[
{label:'option 1',选中:true},
{label:'option 2',选中:false},
{label:'option 3',选中:false},
],
模式:“嵌套”,
};

你有没有偶然发现这个问题?@SpaceOso不幸的是没有。我在故事中为类似的东西创建了一个包装器组件(包装器中的控制道具),但它和我要找的不完全一样。@Ky6uk你能分享一下那个组件包装器最终的样子吗?我正在努力弄清楚myself@MaximFedotov我刚刚在
ButtonComponent.stories.ts
中创建了一个新的Vue组件,比如说
const ControlComponent=Vue.component('ControlComponent'),{components:{ButtonComponent}});
内部的动态属性被
ButtonComponent
变异,然后用它代替
ButtonComponent