Why alias @web blank and not configured?

//Yii::setAlias('web', dirname(dirname(__DIR__)) . '/web'); If you do so it throws out a bunch of //error 404 (site.css main.js).
$this->registerJs('@web/js/main-index.js', // Uncaught SyntaxError: Invalid or unexpected token
$this::POS_HEAD,'main-index');
?>
<h1>main/index</h1>
<p>
 <?= print_r(Yii::$aliases); ?> // @web => empty. 
</p>

The markup looks like this
<script type="text/javascript">@web/js/main-index.js</script>.


The problem was solved.
June 8th 19 at 17:27
2 answers
June 8th 19 at 17:29
Solution
You have obviously confused the 2 step method
$this->registerJs
and
$this->registerJsFile
The first registers the piece of js code and expects the first argument to a string, the second registers the file, and expects a string with the path to the file, including replaces aliases. So the first displays exactly what it needs. Just change the method.
June 8th 19 at 17:31
In General, all of Alice are written in the config, I understand you are trying to specify an alias in the view.
//Yii::setAlias('web', dirname(dirname(__DIR__)) . '/web'); If you do so it throws out a bunch of //error 404 (site.css main.js).

You @web is already defined, but you are trying to override, but a different path dirname(dirname(__DIR__)) . '/web' because Yii will not find the directories css and js from a bunch of 404 errors.

Now on. Read format $this->registerJs, where the first parameter is the JS code. You have the string '@web/js/main-index.js' that is why you get the wrong type
<script type="text/javascript">@web/js/main-index.js</script>


Connect the external js script is in the file assets/AppAsset.php
The view can be explicitly connect assets using AppAsset::register($this)
You can create your own classes to connect and register views.

To get the value of @web, call
echo Yii::getAlias('@web');
That's right, I forgot. I had to write regisrerJsFile. - yasmine1 commented on June 8th 19 at 17:37
Called getAlias without the override, it's empty. - yasmine1 commented on June 8th 19 at 17:34

Find more questions by tags Yii