javascript - 当我使用路由显示 Web 应用程序页面时,如何为模板标签添加 HTML 页面

标签 javascript html reactjs meteor html5-template

我正在学习使用路由连接所有页面的 Udemy 教程。但是,我需要向我的网络应用程序添加一个表单(具有验证等功能)。我发现 aldeed:autoform 但它使用 HTML 模板标签。根据我的理解,我的 React 组件无法渲染出 JSX 中的模板标签,对吗?基本上我需要添加的文本如下所示(取自自动表单文档):

<template name="insertBookForm">
  {{> quickForm collection="Books" id="insertBookForm" type="insert"}}
</template>

是否可以从路由链接到 html 页面?这是我的routes.js 中现在的内容,我需要 /submit 才能呈现该模板表单:

<Router history={browserHistory}>
    <Route onEnter={globalOnEnter} onChange={globalOnChange}>
      <Route path="/" component={HomePage} privacy="unauth"/>
      <Route path="/login" component={Login} privacy="unauth"/>
      <Route path="/signup" component={Signup} privacy="unauth"/>
      <Route path="/submit" component={Submit} privacy="unauth"/>
      <Route path="*" component={NotFound}/>
    </Route>
  </Router>

我的问题有意义吗?我的默认 main.html 看起来像这样:

<head>
  <title>Notes App</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
  <link rel="icon" type="image/png" href="/images/favicon.png"/>
</head>

<body>
  <div id="app"></div>
</body>

我的网络应用程序的完整内容链接到路由(这是在我的 main.js 客户端中):

Meteor.startup(() => {
  ReactDOM.render(routes, document.getElementById('app'));
});

最佳答案

你混淆了 Blaze 和 React。 aldeed:autoform 使用 Blaze 作为 UI 层。如果你想坚持使用纯 React,目前唯一的选择是使用另一个库。制服可能是一个不错的选择:https://github.com/vazco/uniforms

如果您愿意将 Blaze 与 React 一起使用,则可以使用 aldeed:autoform。我自己还没有尝试过,但它看起来类似于:

Meteor 官方“被盗”React tutorial where they use a similar technique to use the Meteor AccountsUI package (which is also built in Blaze) to load the loginButtons using a React component:

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { Template } from 'meteor/templating';
import { Blaze } from 'meteor/blaze';

export default class QuickForm extends Component {
  componentDidMount() {
    // Use Meteor Blaze to render quickForm
    this.view = Blaze.render(Template.quickForm,
      ReactDOM.findDOMNode(this.refs.container));
  }
  componentWillUnmount() {
    // Clean up Blaze view
    Blaze.remove(this.view);
  }
  render() {
    // Just render a placeholder container that will be filled in
    return <span ref="container" />;
  }
} 

关于javascript - 当我使用路由显示 Web 应用程序页面时,如何为模板标签添加 HTML 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45049675/

相关文章:

javascript - 如何在创建 react.js 组件时设置样式?

javascript - 单击添加按钮后未显示提交按钮(AngularJS)

javascript - 边界不适用于 JS 游戏中的球

javascript - CSS 用另一个 div 覆盖 div

javascript - 使用jquery事件委托(delegate)时如何获取最后一个事件

javascript - 目标 =“_blank” 在 Chrome 中不起作用?

javascript - 在查询上下文或传递的 Prop 中找不到 "client"

javascript - 如果 React 组件需要 jQuery,Enzyme 会抛出错误

javascript - 未捕获的类型错误 : Cannot read property 'touched' of undefined

JavaScript 正则表达式接受带有逗号分隔符(千)和点分隔符(小数)的数字