2016-03-07 100 views
1

我不知道是否有方法從服務器加載反應組件模板?Reactjs服務器端模板

當我使用Vuejs或任何其他js庫時,我實際上必須將代碼添加到視圖中,並且在由服務器呈現它之後,javascript開始運行並完成其餘任務。例如,我可以在Symfony應用程序中輸入類似於以下內容的字符串:{{ 'header.article_title'|trans }}並將其翻譯完成。

但是,由於模板被硬編碼爲reactjs組件,我無法再使用php/symfony/twig函數。所以,我想知道是否有辦法從服務器獲取模板,如AngularJS templateURL -option。

+0

https://github.com/mhart/react-server-example? – azium

+0

'webpack chunks'會爲你工作嗎? https://webpack.github.io/docs/code-splitting.html – Skay

回答

1

最後得到如何做我需要的。

我意識到有可能沒有通過URL的實際文件。所以,如果我需要一個/react/component/Article.js它可能是一個行動的網址。

所以,我創建了一個新的捆綁包,添加一個控制器,並使用一個動作來查看單個反應組件。

骨架代碼如下所示:

/** 
* Class ComponentController 
* @package ReactjsBundle\Controller 
* 
* @Route("/react/component") 
*/ 
class ComponentController extends Controller 
{ 
    /** 
    * @Route("/Article.js") 
    */ 
    public function articleAction() 
    { 
     $resp = new Response(); 
     $resp->headers->set('Content-Type', 'text/javascript'); 

     return $this->render('ReactjsBundle:Component:article.html.twig', [], $resp); 
    } 
}