![](/img/trans.png)
[英]Sending props from different routes to the same component using Vue JS
[英]using same props of caller component
我的代碼的目標是使最低級別的組件TextInput.js, Checkbox.js
中的InputGroup.js
可以使用this.props
中的properties
。
為此,我創建了一個非常簡單的名為InputComponent.js
組件,在這里要做的是創建this.prpt
並將this.props
分配給它,以便可以代替TextInput.js
的this.props
使用它。
我似乎很冗長,我認為有更好的方法,但是我想知道是否有更好的方法可以做到這一點。
this.props
,我想on TextInput.js
使用this.props
, on TextInput.js
不是this.props.properties
或this.props.data
。
InputGroup.js
在與傳入的input_type值相對應的組件上工作。
import React, { Component } from 'react';
import {INPUT_TYPES as T} from './constants';
import {TextInput, CheckboxInput, SelectInput, MobileInput, DateInput, SnsAuthInput} from '.';
class InputGroup extends Component {
constructor(props){
super(props);
}
render() {
let input_type = this.props.type;
let switcher = {
[T.TEXT]: TextInput,
[T.CHECKBOX]: CheckboxInput,
[T.SELECT]: SelectInput,
[T.MOBILE]: MobileInput,
[T.DATE]: DateInput,
[T.SNSAUTH]: SnsAuthInput
}
let TagName = (input_type < Object.keys(switcher).length) ? switcher[(input_type)] : undefined;
if (TagName) {
return <TagName properties={this.props} />
}
return <div></div>
}
}
import React, { Component } from 'react';
class InputComponent extends Component {
constructor(props){
super(props);
this.prpt = this.props.properties;
}
}
export default InputComponent ;
import React, { Component } from 'react';
import InputComponent from './InputComponent';
class TextInput extends InputComponent {
constructor(props) {
super(props);
}
render() {
let {input_id, text, isRequired, error} = this.prpt;
return (
<div className="input-group input-text">
<label htmlFor={input_id} className="blind">{text}</label>
<input type="text" id={input_id} placeholder={text} required={isRequired}/>
<span className="id-error">{error}</span>
</div>
);
}
}
export default TextInput ;
[編輯]我想我的問題標題沒有描述要點,所以我更正了它。
[編輯]現在我了解了這一點,因此我更正了標題並添加了眾所周知的解決方案。
```
class InputGroup extends Component {
constructor(props){
super(props);
}
render() {
let input_type = this.props.type;
let switcher = {
[T.TEXT]: TextInput,
[T.CHECKBOX]: CheckboxInput,
[T.SELECT]: SelectInput,
[T.MOBILE]: MobileInput,
[T.DATE]: DateInput,
[T.SNSAUTH]: SnsAuthInput
}
let TagName = (input_type < Object.keys(switcher).length) ? switcher[(input_type)] : undefined;
if (TagName) {
return <TagName {...this.props} />
}
return <div></div>
}
}
我認為您正在尋找類似的東西。
<Parent propX={x} propY={y} />
<Child {...props} />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.