Reactjs 如何在react intl中不在span中包装消息

Reactjs 如何在react intl中不在span中包装消息,reactjs,react-intl,formatmessage,Reactjs,React Intl,Formatmessage,我对yahoo/react intl有一个问题,我想用字符串类型发送消息,但当我使用FormattedMessage时,它会给我用span包装的消息,这并不酷。 我试过格式化消息,但也不起作用。 我非常感谢任何帮助或建议,这是我的代码: import React from 'react'; import {FormattedMessage} from 'react-intl'; export default { items: [ { name: &

我对yahoo/react intl有一个问题,我想用字符串类型发送消息,但当我使用FormattedMessage时,它会给我用span包装的消息,这并不酷。 我试过格式化消息,但也不起作用。 我非常感谢任何帮助或建议,这是我的代码:

import React from 'react';
import {FormattedMessage} from 'react-intl';
export default {
    items: [
        {
            name: <FormattedMessage id='app.dashboard'/>,
            url: '/dashboard',
            icon: 'icon-speedometer',
            badge: {
                variant: 'info',
                text: 'New',
            },
        },
        {
            title: true,
            name:  <FormattedMessage id='app.dashboard'/>,
            // optional wrapper object
            wrapper: {
                // required valid HTML5 element tag
                element: 'strong',
                // optional valid JS object with JS API naming ex: { className: "my-class", style: { fontFamily: "Verdana" }, id: "my-id"}
                attributes: {},
            },
            // optional class names space delimited list for title item ex: "text-center"
            class: '',`enter code here`
        },
从“React”导入React;
从“react intl”导入{FormattedMessage};
导出默认值{
项目:[
{
姓名:,
url:“/dashboard”,
图标:“图标车速表”,
徽章:{
变体:“信息”,
文本:'新',
},
},
{
标题:对,
姓名:,
//可选包装器对象
包装器:{
//必需的有效HTML5元素标记
元素:'强',
//具有JS API命名的可选有效JS对象,例如:{className:“我的类”,样式:{fontfamine:“Verdana”},id:“我的id”}
属性:{},
},
//标题项的可选类名空格分隔列表,例如:“文本中心”
类:“”,请在此处输入代码`
},

如果您自己插入
intl
上下文,那么您就可以使用该函数了

例如,在您的情况下:

items: [
  { 
    name: intl.formatMessage({id:'app.dashboard'});
  }
]
要在组件中获取
intl
,您有两个选择:

  • 从组件的上下文中获取它
  • 用来把它放在你的道具里

  • 如果您不在组件中,这会稍微困难一些,但我会将
    id
    而不是格式化消息放在
    name
    中,然后在可用时使用react intl上下文。在使用并显示此项目列表的组件中。

    用于jsx:

    它被呈现为

    <FormattedMessage id='app.dashboard'/>
    
    <FormattedMessage id='app.dashboard' children={msg=> <option>{msg}</option>}/>
    
    <FormattedMessage id='app.dashboard' children={msg=> <>{msg}</>}/>
    
    或:


    这里的解决方案是将react intl升级到版本3

    在版本3中,
    (和其他类似的react intl组件)被渲染为
    react.Fragment

    如果要将其呈现给其他对象,可以在
    IntlProvider
    上指定
    textComponent
    prop,例如:

    
    

    请参阅中的信息。

    问题不太清楚,您是否可以添加一些更详细的代码Bejgam SHIVA PRASAD看,我的代码在这里工作,但它给了我用span包装的数据,我不喜欢它,formatMessage不工作。。。
    <FormattedMessage id='app.dashboard' children={msg=> <>{msg}</>}/>
    
    <FormattedMessage  id="app.dashboard">{txt => txt}</FormattedMessage>