Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.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 Can';t正确传递段塞变量_Javascript_Reactjs_Graphql - Fatal编程技术网

Javascript Can';t正确传递段塞变量

Javascript Can';t正确传递段塞变量,javascript,reactjs,graphql,Javascript,Reactjs,Graphql,无法正确传递slug变量。当我尝试渲染时,我的组件不会渲染。 我将Post.js中的slug:in和App.js中的slug:slug更改为用于测试的机器字符串,然后才渲染Post.js组件 我告诉我,我没有正确传递slug变量 App.js const App = () => ( <Router> <Header /> <Switch> <Route exact path="/" component={Home} />

无法正确传递slug变量。当我尝试渲染时,我的组件不会渲染。 我将Post.js中的slug:in和App.js中的slug:slug更改为用于测试的机器字符串,然后才渲染Post.js组件

我告诉我,我没有正确传递slug变量

App.js

const App = () => (
<Router>
  <Header />
  <Switch>
    <Route exact path="/" component={Home} />
    <Route path=":slug" component={Post} />
  </Switch>
</Router>
);
export default App;
 const Post = (slug) => (
  <Query query={singlePost} variables={{slug: slug.match.params.urlPath}}>
    {({ loading, error, data }) => {
      if (loading || !data) return <h2>Loading post...</h2>
      if (error) return <h1>Error fetching the post!</h1>
      return (
      <article>
        <h1>{data.articles[0].title}</h1>
      </article>
      )
    }}
  </Query>
)

export const singlePost = gql`
  query singlePost($slug: String!) {
    articles(search: $slug) {
      title
      urlPath
      owner {
        username
      }
    }
  }
`

export default Post;
const-App=()=>(
);
导出默认应用程序;
Post.js

const App = () => (
<Router>
  <Header />
  <Switch>
    <Route exact path="/" component={Home} />
    <Route path=":slug" component={Post} />
  </Switch>
</Router>
);
export default App;
 const Post = (slug) => (
  <Query query={singlePost} variables={{slug: slug.match.params.urlPath}}>
    {({ loading, error, data }) => {
      if (loading || !data) return <h2>Loading post...</h2>
      if (error) return <h1>Error fetching the post!</h1>
      return (
      <article>
        <h1>{data.articles[0].title}</h1>
      </article>
      )
    }}
  </Query>
)

export const singlePost = gql`
  query singlePost($slug: String!) {
    articles(search: $slug) {
      title
      urlPath
      owner {
        username
      }
    }
  }
`

export default Post;
constpost=(slug)=>(
{({加载,错误,数据})=>{
如果(加载| |!数据)返回加载后。。。
if(error)返回获取帖子时出错!
返回(
{data.articles[0].title}
)
}}
)
导出常量singlePost=gql`
查询singlePost($slug:String!){
文章(搜索:$slug){
标题
urlPath
所有者{
用户名
}
}
}
`
导出默认帖子;

解决方案是在路由路径上添加一个
*


slug本身包含正斜杠示例
/new/test/
,但不匹配。params.slug只返回了slug的
/new
部分,并且
*
它阻止了任何正斜杠。

是否尝试在动态路径的开头添加斜杠<代码>@David谢谢你的关注。但这并没有解决问题,只有当我将slug作为如下字符串传递时,我才能动态获取值:
variables={{slug:/home/new post/“}}}
并转到该url。我将
slug.match.params.urlPath
更改为
slug.location.pathname
,现在它工作正常。这是一个好方法吗?