繁体   English   中英

导入 leaflet 单组件苗条

[英]Import leaflet in single component svelte

我在单个组件传单插件中使用,目前我将 js 和 css 放在 public/index.html 但我需要找到一种方法只在单个组件中导入 js 和 ZC7A628CBA22E28EB17B5F5CAAE2 我也尝试过 svelte::head 但它没有用。 此外,这个项目将像 node_modules 一样使用,所以我需要找到一种方法不将 js 和 css 文件放在公共文件夹中。 有什么建议么? 我尝试导入 leaflet 安装 leaflet 与 npm 并导入

import from 'leaflet' 
import * as L from 'leaflet'

但它没有用。

您可以使用 rollup-plugin-css-only 导入 styles。 为了避免丑陋的 css 导入与 node_modules 的相对路径,您可能希望使用 postcss 代替: https://medium.com/@bekzatsmov/how-to-import-css-from-node-modules-in-svelte-app -2a38b50924ff

这是基于标准 Svelte 模板: https://github.com/sveltejs/template

App.svelte

<script>
    import * as L from 'leaflet';
    import '../node_modules/leaflet/dist/leaflet.css'; // It might be better to use postcss.
    import { onMount } from 'svelte';

    let div = null;

    onMount(() => {
        let map = L.map(div, {
            center: [17.385044, 78.486671],
            zoom: 10
        });

        L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map);
    });
</script>

<div bind:this={div} style="height: 100vh; width: 100%;"></div>

main.js

import App from './App.svelte';

const app = new App({
    target: document.body
});

rollup.config.js

import svelte from 'rollup-plugin-svelte';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';
import css from 'rollup-plugin-css-only';

const production = !process.env.ROLLUP_WATCH;

export default {
    input: 'src/main.js',
    output: {
        sourcemap: true,
        format: 'iife',
        name: 'app',
        file: 'public/build/bundle.js'
    },
    plugins: [
        svelte({
            // enable run-time checks when not in production
            dev: !production,
            // we'll extract any component CSS out into
            // a separate file - better for performance
            css: false,
            emitCss: true
        }),
        css({ output: 'public/build/bundle.css' }),

        // If you have external dependencies installed from
        // npm, you'll most likely need these plugins. In
        // some cases you'll need additional configuration -
        // consult the documentation for details:
        // https://github.com/rollup/plugins/tree/master/packages/commonjs
        resolve({
            browser: true,
            dedupe: ['svelte']
        }),
        commonjs(),

        // In dev mode, call `npm run start` once
        // the bundle has been generated
        !production && serve(),

        // Watch the `public` directory and refresh the
        // browser on changes when not in production
        !production && livereload('public'),

        // If we're building for production (npm run build
        // instead of npm run dev), minify
        production && terser()
    ],
    watch: {
        clearScreen: false
    }
};

function serve() {
    let started = false;

    return {
        writeBundle() {
            if (!started) {
                started = true;

                require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
                    stdio: ['ignore', 'inherit', 'inherit'],
                    shell: true
                });
            }
        }
    };
}

export default app;

索引.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset='utf-8'>
    <meta name='viewport' content='width=device-width,initial-scale=1'>

    <title>Svelte app</title>

    <link rel='icon' type='image/png' href='/favicon.png'>
    <link rel='stylesheet' href='/global.css'>
    <link rel='stylesheet' href='/build/bundle.css'>
    <script defer src='/build/bundle.js'></script>
</head>

<body>
</body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM