[英]How do I import JSX into .tsx file (not in React)?
I am not using React . 我没有使用React 。 I am using Stenciljs . 我正在使用Stenciljs 。
I have the following .tsx file: 我有以下.tsx文件:
export class MyComponent {
@Prop() message: string;
render() {
return (<div>{this.message}</div>);
}
}
I want to do this instead: 我想这样做:
import myTemplate from '../my-template.??';
export class MyComponent {
@Prop() message: string;
render() {
return (myTemplate);
}
}
with ../my-template.??
与../my-template.??
containing: 含:
<div>{this.message}</div>
Is it possible and how ? 有可能吗? Thanks in advance for any help :) 在此先感谢您的帮助:)
Yes, you can absolutely do this, there are just a couple of things you need to tidy up: 是的,您绝对可以做到这一点,您需要整理几件事:
Main file 主文件
import { Template } from '../template'; // No need for file extension but we're using a named export so we need the curly braces around 'Template'
export class MyComponent {
@Prop() message: string;
render() {
return ( // You don't technically need the parentheses here as you're just returning one thing
<Template /> // When outputting an imported component, it goes in angle brackets and the backslash closes it like an HTML element
)
}
}
Template 模板
import React from 'react'; // template needs React
export const Template = () => { // defining the export in this way is known as a named export
return (
<p>A message here</p>
)
}
Okay, so that's going to get you a message output which is from your template. 好的,这将使您获得来自模板的消息输出。 However, you were asking about passing a message to that template for it to output. 但是,您在询问有关将消息传递到该模板以使其输出的问题。 That's totally easy as well - you just need to get some props in there. 这也很容易-您只需要在那里放一些道具即可。 Here is the modified version of the above: 这是上述内容的修改版本:
Main file 主文件
import { Template } from '../template';
export class MyComponent {
@Prop() message: string;
render() {
return (
<Template messageToOutput={message} /> // The first argument is the name of the prop, the second is the variable you defined above
)
}
}
Template 模板
import React from 'react';
export const Template = (props) => { // props are received here
return (
<p>{props.messageToOutput}</p> // props are used here
)
}
That's how you pass data around in React - hope that helps! 这就是您在React中传递数据的方式-希望能有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.