首页 文章

蛋糕PHP模板中的Angular 5 App

提问于
浏览
-2

我将使用现有的应用程序构建一个Angular 5应用程序,该应用程序当前正在CakePHP(2.0)上运行,我想使用API调用进行开发,但客户端坚持在蛋糕模板中加载Angular5应用程序并使用Cake会话中的一些信息,如current用户信息而不是从服务器拉 . 有没有什么办法可以将Angular 5 App作为Angular缓存HTML模板的蛋糕模板,如果有可能,我真的很感激任何帮助材料 .

1 回答

  • 0

    你的所有DOMElement必须由Angular自己管理 . 在纯html部分,您只需添加boostraped组件标记,例如:

    <app-root></app-root>

    然后,正如@karol所解释的那样,您可以向您的应用程序提供如下信息:

    // Rest of your template
    <script>
    window.myAppNamespace = {
        post : <?= json_encode($myPostData) ?>  
    };
    </script>
    // Rest of your template
    

    从技术上讲,对于上面解释的信息,您可以使用任何渲染引擎,例如Cake PHP引擎 .

    然后在你的Angular应用程序上,从任何地方(但更喜欢服务)你可以做类似的事情:

    我假设你的结构是这样的

    export interface PostModel {
        id: number;
        title: string;
        comments: Array<{
            author : string;
            content: string;
        }>;
    }
    @Injectable()
    export PostService {
        private post: PostModel;
    
        constructor() {
            this.post = (window.myAppNamespace as any).post;
            // From here you can use your data provided by your CakePHP app.
        }
    }
    

相关问题