[英]Import a working javascript file in Svelte
I'm building an app in Svelte and I'm trying to add to it a previously written .js
file.我正在 Svelte 中构建一个应用程序,并尝试向其中添加一个以前编写的.js
文件。 I tried to import it in the main.js
file, but gives me an error: Cannot read properties of null (reading 'offsetHeight')
This is the .js
file:我试图在main.js
文件中导入它,但给了我一个错误: Cannot read properties of null (reading 'offsetHeight')
这是.js
文件:
[...]
function colorSubheadings() {
// Replace subheading background colors
var redStart = 255;
var greenStart = 255;
var blueStart = 255;
var redEnd = 249;
var greenEnd = 250;
var blueEnd = 251;
var redDiff = redEnd - redStart;
var greenDiff = greenEnd - greenStart;
var blueDiff = blueEnd - blueStart;
var page = document.querySelector('.page');
var pageHeight = page.offsetHeight;
var subheadings = document.getElementsByClassName('.chapter');
for (var i = 0; i < subheadings.length; i++) {
// Get the position relative to .page
var span = subheadings[i].querySelector('span');
var factor = span.offsetTop / pageHeight;
var r = Math.floor(redDiff * factor + redStart);
var g = Math.floor(greenDiff * factor + greenStart);
var b = Math.floor(blueDiff * factor + blueStart);
var color = 'rgb(' + r + ',' + g + ',' + b + ')';
// Color background based on position
span.style.backgroundColor = color;
span.style.boxShadow = '11px 0 0 ' + color + ', -13px 0 0 ' + color;
}
}
[...]
And this is the .svelte
file:这是.svelte
文件:
<script>
//some functions
</script>
<div class="page">
[...]
<div class="chapter-sidebar">
<div class="sidebar js-sidebar">
<div class="sidebar__wrapper">
<ul class="sidebar__list">
{#each files as { file } (file.name)}
<li class="sidebar__list-item">
<a class="sidebar__link" href="#temp">{file.name.split('.').slice(0, -1).join('.')}</a>
</li>
{/each}
</ul>
</div>
</div>
In the .svelte
file there is a lateral navbar, just like this .在.svelte
文件中有一个横向导航栏,就像这样。 In vanilla html it works perfectly, but with svelte there are some bugs, like the navBar doesn't "stick" in a place: if I scroll down it remains at the top of the page, insted of remain in a particuar position of the screen, "following" the user's scroll.在原版 html 中,它可以完美运行,但是在 svelte 中存在一些错误,例如导航栏不会“粘”在某个地方:如果我向下滚动,它仍会保留在页面顶部,并保留在特定的 position 中屏幕,“跟随”用户的滚动。 So, what can i do to use this .js
file in my svelte project?那么,我该怎么做才能在我的 svelte 项目中使用这个.js
文件呢? I also tried this solution, but it didn't worked for me.我也尝试了这个解决方案,但它对我不起作用。 Thanks in advace.预先感谢。
Where is the colorSubheadings()
function called? colorSubheadings()
function 在哪里调用?
(I see var subheadings = document.getElementsByClassName('.chapter')
but no elements with class 'chapter' in the Svelte file) (我看到var subheadings = document.getElementsByClassName('.chapter')
但在 Svelte 文件中没有包含 class 'chapter' 的元素)
I suggest you either try我建议你要么尝试
index.html
in yourproject/puplic
folder after the <script src="/build/bundle.js"></script>
(in case the function is called inside the same .js
file)在<script src="/build/bundle.js"></script>
之后在yourproject/puplic
puplic 文件夹中的index.html
中导入 .js 文件(以防 function 在同一个.js
文件中被调用).svelte
component ( .js
file inside yourproject/src
folder with exported function export colorSubheadings() {...}
)在.svelte
组件的onMount中运行 function (您的yourproject/src
文件夹中的.js
文件,导出 function export colorSubheadings() {...}
) import {onMount} from 'svelte'
import {colorSubheadings} from './xy.js'
onMount(()=> {
colorSubheadings()
})
You need to export the function in your js file before you can import it in your.svelte file (or other JS file).您需要先将 function 导出到您的 js 文件中,然后才能将其导入到您的 .svelte 文件(或其他 JS 文件)中。
export function colorSubheadings() {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.