Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

import into the javascript frontend(html javascript)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First frontend = browser, backend = nodejs. You surely mean that LowDB is available as a NodeJS module, which means it can be installed with NPM, either for frontend or backend development.

If you're not using NPM for your frontend development, you can still import modules with <script> tags.

Let's see an example using only HTML/javascript:

(from the doc):

<html>
    <head>
        <script src="https://unpkg.com/lodash@4/lodash.min.js"></script>
        <script src="https://unpkg.com/[email protected]/dist/low.min.js"></script>
        <script src="https://unpkg.com/[email protected]/dist/LocalStorage.min.js"></script>
    </head>
    <body>

        <button onclick="add()">Add Post</button>
        <button onclick="load()">Load Post</button>
        <div></div>
    </body>
    <script>
        var adapter = new LocalStorage('db')
        var db = low(adapter)
        db.defaults({ posts: [] })
            .write()
        function add() {
          // Data is automatically saved to localStorage
          db.get('posts')
            .push({ title: 'lowdb' })
            .write()
        }
        function load() {
          var div = document.querySelector('div')
          var text = div.innerText;
          text += db.get('posts').map(post=>post.title).join(';');
          div.innerText = text 
        }
    </script>

</html>

See this work: https://codepen.io/bcaure/pen/ExgBPBz


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...