2015-04-06 27 views
2

我在加载布局文件的AppAsset中拥有所有CSS和JavaScript文件。在我的登录页面上,我不想使用布局,因此我使用 renderPartial() 方法呈现登录视图文件。当我这样做时,我创建了一个新的LoginAsset文件,并用它来注册登录视图,但仍然没有加载我的CSS和JS。我也尝试使用AppAsset包注册登录视图,但它也不会加载CSS和JS。有什么我做错了吗?在我的控制器上使用renderPartial时,如何加载我的assetbundle?yii2:当我使用renderPartial方法加载页面时,资源包不会加载资源

+0

注册此资产包的位置?看起来像在布局中,它不是用'renderPartial'呈现的。 – arogachev 2015-04-06 04:38:15

+0

你尝试过“renderAjax”方法吗? – 2015-04-06 04:49:02

+0

我在布局中注册资产捆绑包,但我试图在登录视图中注册相同的资产捆绑包。我也尝试创建一个新的资产包,并在登录视图页面上注册它,但仍然无法加载。 – Zack 2015-04-06 04:49:42

回答

2

嗨@Zack你的问题是你没有指定end body和end page的位置。 我解决了我的问题,将这些代码添加到我的页面底部。

<?php $this->endBody() ?> 
</body> 
</html> 
<?php $this->endPage() ?> 
0

由于Pavel said你shold使用renderAjax()。见下面的解释。

renderPartial()

输出是没有任何CSS ANS JS通过视图生成的HTML的和平:

<div class="container"> 
    <input type="text" id="loginform-username" class="form-control" name="LoginForm[username]"> 
    <input type="password" id="loginform-password" class="form-control" name="LoginForm[password]"> 
    <button type="submit" class="btn btn-primary btn-block" name="login-button">Log in</button> 
</div> 

renderAjax()在视图增加CSSS和JSS所有registred 资产输出:

<link href="/assets/f1759119/css/bootstrap.css" rel="stylesheet"> 
<div class="container"> 
    <input type="text" id="loginform-username" class="form-control" name="LoginForm[username]"> 
    <input type="password" id="loginform-password" class="form-control" name="LoginForm[password]"> 
    <button type="submit" class="btn btn-primary btn-block" name="login-button">Log in</button> 
</div> 
<style> 
    ... 
</style> 
<script> 
    ... 
</script> 
<script src="/assets/13aa7b5d/jquery.js"></script> 
<script src="/assets/302a2946/yii.js"></script> 
<script src="/assets/302a2946/yii.activeForm.js"></script> 

PS。我认为正确的方法是为登录表单创建单独的布局,并使用通常的render()方法。