希望得到:pages/system/dictionaries/index.vue
console.log(__filename, __dirname); 也拿不到
但是在vue.config.js就可以:
window.location.pathname
在JavaScript中,你可以使用window.location.href来获取当前文件的地址。这将返回一个字符串,包含了完整的URL。
例如:
var currentFileUrl = window.location.href;
console.log(currentFileUrl);
这将会在控制台输出当前文件的URL。
需要注意的是,如果你在iframe或者窗口中打开一个新页面,window.location.href将返回新页面的URL,而不是原始页面的URL。
如果你想获取当前执行的JavaScript文件的路径,可以使用__filename或__dirname(这取决于你是在Node.js环境还是浏览器环境)。但在浏览器环境中,__filename将返回包含文件名的URL,而不是完整的文件路径。
例如:
var currentFile = __filename; // 在浏览器环境中
console.log(currentFile);
如果你需要获取完整的文件路径,这在浏览器环境中通常是不可能的,因为出于安全和隐私的考虑,浏览器不允许脚本访问完整的文件路径。
【以下回答由 GPT 生成】
在React.js中,你可以使用window.location对象来获取当前文件的地址。具体步骤如下:
import React from 'react';
import window from 'window';
const currentPath = window.location.pathname;
const currentURL = window.location.href;
const fileName = currentURL.split('/').pop();
整体代码如下:
import React from 'react';
import window from 'window';
const MyComponent = () => {
const currentPath = window.location.pathname;
const currentURL = window.location.href;
const fileName = currentURL.split('/').pop();
return (
<div>
<p>当前文件路径:{currentPath}</p>
<p>当前文件完整地址:{currentURL}</p>
<p>当前文件名:{fileName}</p>
</div>
);
};
export default MyComponent;
请注意,以上代码假设你已经安装了window包,并正确引入。另外,这种解决方案仅适用于基于浏览器的React应用程序,不适用于服务器端渲染的应用。如果你的项目结构或文件地址格式有特殊情况,你可能需要对代码进行适当的修改。
【相关推荐】