404错误页面未找到(404 Error)-Yii 2

np8igboo  于 2022-11-09  发布在  其他
关注(0)|答案(3)|浏览(152)

我创建了Yii 1应用程序,并使用以下脚本在视图文件中导航:

$(function() {
    $('a[href*="#"]:not([href="#"])').click(function() {
        if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
            var target = $(this.hash);
            target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
            if (target.length) {
                $('html, body').animate({
                    scrollTop: target.offset().top
                }, 1000);
                return false;
            }
        }
    });
});

但是,当我创建yii2应用程序并粘贴这段代码时,它并不工作。

$(function() {
        $('a[href*="#"]:not([href="#"])').click(function() {
            if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
                var target = $(this.hash);
                target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
                if (target.length) {
                    $('html, body').animate({
                        scrollTop: target.offset().top
                    }, 1000);
                    return false;
                }
            }
        });
    });

我使用以下代码在ThemeAsset中注册了此代码:

public $js = [
 'Index/menu_navigate.js'
]

但是,这个代码没有帮助我,它是不工作的。我无法设法找到任何错误。在控制台屏幕上显示以下错误消息
未找到页面(未找到)

lokaqttq

lokaqttq1#

请使用'/Index/menu_navigate.js',这将帮助您获取正确路径

eyh26e7m

eyh26e7m2#

如果此文件位于Web可访问目录之外,则需要在资产包中设置正确的sourcePath

<?php

namespace app\assets;

use yii\web\AssetBundle;

class MenuNavigationAsset extends AssetBundle 
{
    public $sourcePath = '@bower/font-awesome'; // Replace with folder where "Index" folder is located. Path should be absolute, you can use aliases here.
    public $js = [ 
        'Index/menu_navigate.js', 
    ];    
}

然后它将被复制到web可访问的目录并从那里加载。你可以为定义别名here找到官方文档。它可以防止你硬编码绝对路径。
否则(如果文件位于可通过Web访问的目录中),请设置basePathbaseUrl属性:

<?php

namespace app\assets;

use yii\web\AssetBundle;

class MenuNavigationAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $js = [
        'Index/menu_navigation.js',
    ];   
}

请准确检查浏览器尝试加载此js文件的路径,如果路径正确,请确保该文件存在于该路径中,否则请在您的资产包中更正路径。
同时检查路径是否有打字错误。
使用资产的官方文档here可用。

aelbi1ox

aelbi1ox3#

你应该把css或js文件放到web文件夹的js,css中,然后把file -〉

class BackendAsset extends AssetBundle
{
    public $basePath='@webroot';
    public $baseUrl='@web';
    public $css=[
        'css/style.css'
    ];
    ...... `code of you`
}

相关问题